onStop/Restart/Start/Resume

Put your problem here if it does not fit any of the other categories.

onStop/Restart/Start/Resume

Postby Kalder » Sun Jan 29, 2012 5:37 pm

Hi guys, I'm still working on my app, first i'll explain what it does and then what i want it to do.

When the app is started, it shows a splash screen with a button, after this button is clicked the user gets a view with 3 tabs, at the moment only the first tab has content, and the selected tab is the first tab which shows a listview with RSS news articles. When a user clicks on the article they will be sent to the corresponding url.
However, when the user has read enough, and wants close the app, I assume they close it by hitting the hardware back button. It then hides the app since I set up the splash screen to finish itself after being displayed.

Now the problem, when the user starts the app again, the splashscreen is displayed again, this i do not want, unless the app has actually been killed, it should just show the tabs view.

Another problem i run into when the app is started for the second time (or resumed, i don't know which it does), is that when the splashscreen button is pressed for the second time it force closes the app because it starts parsing the RSS for a second time giving me arrayoutofbounds errors. It should either memorize the complete listview with rss items, or it should reset completely to be able to parse it again.


my set up is as follows:

splashactivity shows,
Tabacitivty shows RSSactivity in the first tab.
RSSactivity starts RSSreader and RSShandler to parse an rss feed and build a listview.


Any help on how to set up the onStop onStart onRestart onResume onPause etc methods would be greatly appreciated.


(All i have set up now is onCreate methods and an onStop method in the splashactivity which only does finish())
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Top

Re: onStop/Restart/Start/Resume

Postby Phyll » Sun Jan 29, 2012 8:06 pm

Hi Kalder,

Making progress.

About the first question. You need to put something like this in your code to return to (actually restart the) home screen.

Code: Select all
               Intent startMain = new Intent(Intent.ACTION_MAIN);
                startMain.addCategory(Intent.CATEGORY_HOME);
                startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(startMain);


When you call finish in an activity it removes itself from the activity stack. If its at the top of the stack, it ends the app. Since your splash screen ends itself, that leaves your main activity for the reader on the top of the stack. This code leaves it there and starts the home screen for you to choose other apps from. You must intercept the back key to run this code instead. Question then is, how do you end the program so the splash screen can start and reread the feed? Probably need a special button to restart the app with an intent and then finish this one.

Whenever you change tabs, the current tab activity is paused. When you return, it is resumed. Put anything that needs fixed after a pause in the resume not in the oncreate. The easiest way to see how your app behaves is to put plenty of Log statements in there at all the points you want to track, like oncreate, pause, resume and any of those other ones you might be using. Then you can see the sequence during these state changes. Also, if you are using the application global variables you need to handle saving them yourself since the activity manager does not help you do this. Be aware though that while your app is running this should not be a concern. This will only happen when the OS needs the memory very badly and almost kills your app completely.

Hope this helps.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: onStop/Restart/Start/Resume

Postby Kalder » Sun Jan 29, 2012 8:39 pm

in the tabactivity, i added:
Code: Select all
@Override
   public void onBackPressed() {
      // TODO Auto-generated method stub
      super.onBackPressed();
      Log.d("Back Pressed?", "Yes");
      Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
   }

however, when i press the back button, my log message doesn't show, so it does not start the new intent, seems like it bypasses onBackPressed() although it shouldn't.

I'm going to post the contents of the splash activity, tabsactivity and rssactivity, it will make helping me easier i guess ;)

Splash Activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.tmm.android.rssreader;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8.  
  9. public class startSplash extends Activity {
  10.  
  11.         @Override
  12.         protected void onStop() {
  13.                 // TODO Auto-generated method stub
  14.                 super.onStop();
  15.                 finish();
  16.         }
  17.  
  18.         @Override
  19.         public void onCreate(Bundle savedInstanceState) {
  20.                 // TODO Auto-generated method stub
  21.                 super.onCreate(savedInstanceState);
  22.                 setContentView(R.layout.splash);
  23.  
  24.                 final Thread StartApp = new Thread() {
  25.                         public void run() {
  26.                                 Intent intent = new Intent(startSplash.this, TabsActivity.class);
  27.                                 startActivity(intent);
  28.                         }
  29.                 };
  30.  
  31.                 Button bt1 = (Button) findViewById(R.id.button1);
  32.                
  33.                 bt1.setOnClickListener(new View.OnClickListener() {
  34.                         @Override
  35.                         public void onClick(View v) {
  36.                                 // TODO Auto-generated method stub
  37.                                 StartApp.start();
  38.                         }
  39.                 });
  40.                
  41.         }
  42. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


TabsActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.tmm.android.rssreader;
  2.  
  3. import android.app.TabActivity;
  4. import android.content.Intent;
  5. import android.content.res.Resources;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.widget.TabHost;
  9.  
  10. public class TabsActivity extends TabActivity {
  11.         @Override
  12.     public void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.main);
  15.  
  16.         Resources res = getResources(); // Resource object to get Drawables
  17.         TabHost tabHost = getTabHost();  // The activity TabHost
  18.         TabHost.TabSpec spec;  // Reusable TabSpec for each tab
  19.         Intent intent;  // Reusable Intent for each tab
  20.  
  21.         // Create an Intent to launch an Activity for the tab (to be reused)
  22.         intent = new Intent().setClass(this, RssActivity.class);
  23.  
  24.         // Initialize a TabSpec for each tab and add it to the TabHost
  25.         spec = tabHost.newTabSpec("nieuws").setIndicator("Nieuws",
  26.                           res.getDrawable(R.drawable.ic_tab_rss))
  27.                       .setContent(intent);
  28.         tabHost.addTab(spec);
  29.  
  30.         // Do the same for the other tabs
  31.         intent = new Intent().setClass(this, StandActivity.class);
  32.         spec = tabHost.newTabSpec("stand").setIndicator("Stand",
  33.                           res.getDrawable(R.drawable.ic_tab_stand))
  34.                       .setContent(intent);
  35.         tabHost.addTab(spec);
  36.  
  37.         intent = new Intent().setClass(this, FeyenoordActivity.class);
  38.         spec = tabHost.newTabSpec("feyenoord").setIndicator("Feyenoord",
  39.                           res.getDrawable(R.drawable.ic_tab_feyenoord))
  40.                       .setContent(intent);
  41.         tabHost.addTab(spec);
  42.  
  43.         tabHost.setCurrentTab(0);
  44.     }
  45.  
  46.         @Override
  47.         public void onBackPressed() {
  48.                 // TODO Auto-generated method stub
  49.                 super.onBackPressed();
  50.                 Log.d("Back Pressed?", "Yes");
  51.                 Intent startMain = new Intent(Intent.ACTION_MAIN);
  52.         startMain.addCategory(Intent.CATEGORY_HOME);
  53.         startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  54.         startActivity(startMain);
  55.         }
  56. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


RssActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.tmm.android.rssreader;
  2.  
  3. import java.net.URL;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import org.json.JSONObject;
  7.  
  8. import android.app.ListActivity;
  9. import android.content.Intent;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.widget.ListView;
  14.  
  15. import com.tmm.android.rssreader.reader.RssReader;
  16. import com.tmm.android.rssreader.util.RSSHandler;
  17.  
  18. public class RssActivity extends ListActivity {
  19.  
  20.        
  21.         private RssListAdapter adapter;
  22.  
  23.         /** Called when the activity is first created. */
  24.         @Override
  25.         public void onCreate(Bundle savedInstanceState) {
  26.                 super.onCreate(savedInstanceState);
  27.                
  28.                 List<JSONObject> jobs = new ArrayList<JSONObject>();
  29.                 jobs = RssReader.getLatestRssFeed();
  30.                 /*
  31.                  * try {
  32.                  *
  33.                  * } catch (Exception e) { Log.e("RSS ERROR",
  34.                  * "Error loading RSS Feed Stream >> " + e.getMessage() + " //" +
  35.                  * e.toString()); }
  36.                  */
  37.                 /*
  38.                
  39.                 */
  40.  
  41.                 adapter = new RssListAdapter(this, jobs);
  42.                 setListAdapter(adapter);
  43.                 setContentView(R.layout.nieuws_layout);
  44.  
  45.         }
  46.        
  47.  
  48.         @Override
  49.         protected void onListItemClick(ListView l, View v, int position, long id) {
  50.                 // TODO Auto-generated method stub
  51.                 super.onListItemClick(l, v, position, id);
  52.  
  53.                 URL webURL = RSSHandler.UrlArray[position];
  54.                 Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(webURL
  55.                                 .toString()));
  56.  
  57.                 startActivity(myIntent);
  58.  
  59.         }
  60.  
  61. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Re: onStop/Restart/Start/Resume

Postby Phyll » Sun Jan 29, 2012 8:51 pm

Hi Kalder,

Code: Select all
    @Override
    public void onBackPressed() {

       //TODO: Task goes here. No task will ignore back button

        return;
        }//onbackpressed


Don't use the super, espcially before what you really want to do. I'll look at the rest of you code.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: onStop/Restart/Start/Resume

Postby Kalder » Sun Jan 29, 2012 11:22 pm

Instead of placing this in the TabActivity:
Kalder wrote:
Code: Select all
@Override
   public void onBackPressed() {
      // TODO Auto-generated method stub
      super.onBackPressed();
      Log.d("Back Pressed?", "Yes");
      Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
   }


I placed this in the RssActivity:
Code: Select all
@Override
   public void onBackPressed() {
      // TODO Auto-generated method stub
      Log.d("Back Pressed?", "Yes");
      Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
        return;
   }

And now it works :D

Thanks for that piece of code Phyll! It's exactly what i was looking for.
Now I will try to make a menu that allows the user to really close the app :)
Kalder
Junior Developer
Junior Developer
 
Posts: 17
Joined: Sun Jul 24, 2011 8:20 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: MSNbot Media, Yahoo [Bot] and 19 guests