Maintaining standard application Activity back stack state in Android (using singleTask launch mode)

StackOverflow https://stackoverflow.com/questions/5086985

سؤال

I am having trouble finding out how to maintain the state of my Android app in development.

Just to clarify, I am not talking about maintaining activity state (i.e. keeping track of textbox values, checkboxes, etc on a specific activity).

Let's say for example my application has two activities A and B. When I start my app, it takes me to activity A, and pressing a button on it takes me to activity B. At this point, I press the home button on my phone to return to the main Android UI and exiting my app . However, if I choose to run my app again, it should take me to activity B, which is where I left off before pressing the home button, but instead it is taking me to activity A.

Does anyone know how I can rectify this?

(I am using a Samsung Vibrant in case if you need to know)

هل كانت مفيدة؟

المحلول

"However, if I choose to run my app again, it should take me to activity B, which is where I left off before pressing the home button, but instead it is taking me to activity A."

Yes, it should. If it isn't, you have done something in your app to tell the platform to modify its behavior. (Look at ApiDemos as an example, it uses the standard behavior, which is what it sounds like you are describing as what you expect.)

Things to look out for:

  • Don't use FLAG_ACTIVITY_NEW_TASK when launching activities.
  • Don't use the singleTask or singleInstance launch modes.
  • Don't see the clearTaskOnReset flag.

نصائح أخرى

You're imagining there's something called an "Application" but that's an illusion. Your application is just a collection of Activities, Services, Receivers, etc.

If you look at the intent-filter tags in your manifest, you'll see that each icon in the home screen is associated with a filter like this:

     <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>

You can put that same chunk of XML on both of your Activities, and you'll get two icons in the home screen, one that always launches Activity A, and one that always launches Activity B.

What you may want to do instead is create a master Activity that launches one of the other Activities based on the shared state.

As for where to actually store the shared state, that depends on how complex your state is. This is a good place to start: http://developer.android.com/guide/topics/data/data-storage.html

As I understand the question you want to launch your application and have a different thing happen each time depending on where you left off the last time. http://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html The activity lifecycle is in the link. Your onActivityDestroyed method somehow needs to persist the present state and the oncreate needs to pick it back up. Persistence can be achieved via shared preferences, stored in a file, database or over the network http://developer.android.com/guide/topics/data/data-storage.html This unpredictable behavior could cause confusion for the end user if poorly implemented so use good judgement.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top