Skip to content Skip to sidebar Skip to footer

Open Last Activity When The App Is Killed

then in my app there are 103 activity, in sequence SplashActivity -> Menu ---> Q_001 ---> Q_002 ... (...) ..... Q_finish. in each of the activity I have a button to put ex

Solution 1:

I agree with @2Dee.

First thoughts were that your are not really learning the current Android development, I think. I suppose most people would go for one activity for splash (if you really need one) and a second activity for holding the 100 buttons. This 100 buttons would be hold inside a viewpager. It is a bit more complex to understand at first but it is the way to go, in my opinion. The view pager will recycle, is efficient and a powerful tool to leverage.

But maybe you are doing a school exercise ?

Edit: I see 2 ways, probably someone can find better.

  1. You can save in memory where the user is. When the user click to exit, just save in a SharedPreferences the current question and retrieve it at start : sharedPreferences
  2. You can make that clicking exit button jsut call onBackPressed() and maybe override onBackPressed. If you do that the activity will be send to background and the user to Home. If he goes back to it, it will restart on that last activity. You must still save it in case the user kill the app or Android kills the app for ressource management.

Edit 2 :

In Splash Activity, you retrieve an integer. in the main activity (the one holding the question and the buttons) you write the question number.

That gave something like that :

splash activity :

@OverrideprotectedvoidonCreate(Bundle state){
   super.onCreate(state);
   . . .

   // Restore preferencesSharedPreferences settings = getSharedPreferences(LAST_QUESTION, 0);
   int number = settings.getInt("last_question", 1); // default value is to start againgoToQuestionNumber(number); // you better call this in onResume(). 
}

In the exiting method launched by the onClik()

SharedPreferencessettings= getSharedPreferences(LAST_QUESTION, 0);
  SharedPreferences.Editoreditor= settings.edit();
  editor.putInt("last_question", currentQuestionNumberThatYouShouldKnow);

Solution 2:

Ok, as I thought, I believe you need to rethink your app's architecture :)

Make one Activity and one Fragment. The Activity will be responsible for switching the Fragment in view and creating them. It will also hold a reference to the urls necessary for the webviews.

This is the way I would do it (but it might take a complete refactoring of your code - which is not a bad exercise if you think about it) :

in Activity :

// declare your urls in the res/values/strings.xml and use them here.privateint[] webViewsUrls = {R.string.url1, R.string.url2, ...}; 

in Activity's onCreate :

SharedPreferencesprefs= getSharedPreferences(PREFS_NAME, 0);
intactivityIndex= prefs.getInt("lastActivityIndex", 0);
QuestionFragmentfragment=newQuestionFragment();
Bundlebundle=newBundle();
bundle.putInt("INDEX", activityIndex);
bundle.putString("WEBVIEW_URL", webViewsUrls[activityIndex]);
fragment.setArguments(bundle);
// Show fragment

To add a new Fragment to a ViewGroup inside your Activity layout (if you don't use a ViewPager but rather use a button to change the question in view) :

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
// create you fragment using the method I give above
fragmentTransaction.add(R.id.fragment_container_in_activity_layout, fragment, STRING_FRAGMENT_TAG);
fragmentTransaction.commit();

To replace the fragment already in view, use the replace method instead of add.

The on click on a button, or in a ViewPager, change the Fragment in view and do not forget to save the new value for activityIndex in SharedPreferences when starting your Fragment.

I believe the suggestion of @Poutrathor is quite good, you could refer to this answer I gave yesterday if you want to use ViewPager, which I think would fit your requirements better than having 100+ Activities.

Post a Comment for "Open Last Activity When The App Is Killed"