Reputation: 95
I have an problem. I have two activities - MainActivity and ActivityB
a. First activity (MainActivity) has one button which starts ActivityB
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent activityB = new Intent(MainActivity.this, activityB.class);
startActivity(activityB);
}
});
b. Second activity has few Text Fields
When I click back button in ActivityB then app switch to MainActivity, but I dont finish() ActivityB.
How can I programme button on MainActivity which will check if ActivityB was already created. If yes just switch to ActivityB (maybe with some typed data in text fields), else create ActivityB and switch to it ???
At this moment every click to button creates new activity (new Text Fields and so on, so user loose wroten before data).
I tried
android:launchMode="singleInstance"
and flags FLAG_ACTIVITY_REORDER_TO_FRONT...
Upvotes: 3
Views: 339
Reputation: 7474
ActivityA -> ActivityB -> ActivityA (reused)
in ActivityB while starting ActivityA:
use FLAG:
/**
* When an existing singleTask activity is launched,
* all other activities above it in the stack will be destroyed.
* It's different when launchMode is "Standard".
* The task which contains flag will come to the foreground
* and keep the state the same as before.
* When you press HOME and launch the activity again,
* ActivityManger calls an intent
* {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
* flag=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED cmp=A}
*/
Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP;
and in ActivityA override:
/**
* This is called for activities that set launchMode to "singleTop" in
* their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
* flag when calling {@link #startActivity}. In either case, when the
* activity is re-launched while at the top of the activity stack instead
* of a new instance of the activity being started, onNewIntent() will be
* called on the existing instance with the Intent that was used to
* re-launch it.
*
* <p>An activity will always be paused before receiving a new intent, so
* you can count on {@link #onResume} being called after this method.
*
* <p>Note that {@link #getIntent} still returns the original Intent. You
* can use {@link #setIntent} to update it to this new Intent.
*
* @param intent The new intent that was started for the activity.
*
* @see #getIntent
* @see #setIntent
* @see #onResume
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
/** when we are back check here intent action for example or extras */
}
Thats not what I mean. Please read one more time: How can I programme button on MainActivity which will check if ActivityB was already created. If yes just switch to ActivityB (maybe with some typed data in text fields), else create ActivityB and switch to it ??? - Juliusz Hajdacki
@juliusz-hajdacki yes this is exactly what u want :)
return result to first activity (via start activity with extras)
Upvotes: 3