Parent activity onCreate() called on back press (Android) - android

I have 3 activities - A, B and C.
From A I start B, and from B I start C, i.e
A -> B -> C.
On going from A to B, I deliver an intent to B - let's call this intent intentA.
When I go from B to C, and then from C to B by pressing back button, 'onCreate()' of B is called again and intentA is delivered to B again, even though I didn't pass any intent to B while pressing back button in C.
What do I do so that 'onCreate()' of B is not called when pressing back button in C, or even if it is called, intentA is not delivered to it?
I tried setting launchmode of B to 'singletop'/'singletask' in manifest, but they didn't work.
I tried using 'finish()' in onBackPressed() method in C, but didn't work.
I tried other solutions as well using answers to similar questions on stackoverflow, but none of them worked.
Any solution?
Also, I'm using Dexter library to ask for permissions in my custom dialogbox, and I'm using screen transition animations as well. Could these be causing the issue?

For launching ActivityC from ActivityA with adding ActivityB to backstack,
use startActivities with array of intentA and intentB as parameter

Related

Go back to Activity without recreating (without invoking onCreate())

I would like to ask similar question to:
Go back to previous screen without creating new instance
However I don't want Activity A go through onCreate callback, I would like to return to previous instance in the same state. How to achieve that without calling multiple finish() ?
There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:
A --> B --> C -->D
where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.
1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.
<activity
android:name=".ui.MyActivity"
android:launchMode="singleTask"/>
But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like
A--> B
2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.
When navigating from A to B do not finish Activity A.
When navigating from B to C finish Activity B.
When navigating from C to D finish Activity C.
So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

How to go back to Home Activity from a specific activity without overriding back button

I am new to Android . Here I have for activities A,B,C,D in which A is the Home Activity.It is in stack as A->B->C->D
When I press back from B or C it should go back just as normal. But if I press back from D it should go back to A and from A the app should exit
I guess you could intercept onStop() and guessing if the activity is switching to C and launching A instead. But it would result in a hard to maintain mess and I do not recommend that.
However, if for some reason you still not want to override onBackPressed and you manage to guess that D is stopping because back was pressed (without overriding onBackPressed(), just start A activity from there with an Intent with FLAG_ACTIVITY_CLEAR_TOP (call i.setFlags(FLAG_ACTIVITY_CLEAR_TOP) )
According to the doc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So A will be brought back and B and C will be cleared.
when you start your new activity finish the old activity for example if you want to start C from B :
Intent I = new Intent(B.this , C.class) ;
startActivity (I);
B.this.finish();

clear back stack of activities

I have some activities A, B, C, D. No the way it's set up is like this.
When user starts the app, activity A starts.
Based on a preference, which if set to true, immediately starts activity D
From there D starts C and C starts B which then starts A
... At this point i want D, C, B removed from the back stack so that user cant go back to them by pressing back from A (but the back button should work like it should when in D, B, C).
so to sum it up i need something like this
D <--> C <--> B --> A
I tried using intent flags Intent.FLAG_ACTIVITY_CLEAR_TOP & Intent.FLAG_ACTIVITY_NEW_TASK
but they dont work.
How do i accomplish this??
Then start activity A with FLAG_ACTIVITY_TASK_ON_HOME flag set.
Ok.
How about calling finish on B after launching A?

Getting back to the first activity via intent, what happens to the activity stack?

I have an application which pulls data from a webserver, putting this data in a listview and then presenting it to the user. There are 4 activities involved, which can be called like this:
A -> B -> C -> D
or
A -> B -> D
Basically all the activities except A are pulling data from the web. Should there be any problems with the connection and there is a timeout coming up I want the activities B, C and D to inform the user and get back to A.
So what I did right now is, I set A to launch mode singleTask. This way I can catch the timeout exception and call a new intent starting A. But what happens to the activities in between? Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A, but what about B and C? Does android automatically call onDestroy on these? What happens to the activity stack? Any hints appreciated.
Cheers
When you launch activity A, from activity D, set the intent flag:
FLAG_ACTIVITY_CLEAR_TOP
Using this flag will clear any activities in between A and the activity you are in, bringing A to the front. You also probably don't need to be using singleTask as a launch mode.
Let's say I am calling A->B->C->D and then in D the connection times out. Now the app is going back to A...
With your example, if all the child activities are launched with startActivityForResult() and activities B & C implement:
finishFromChild(Activity activity) {
...
finish();
}
When activity D calls finish() after it has timed out, then each child will close in order (D -> C -> B -> A) with a chance to return any relevant data you might want to salvage.

Moving between activities: killing two previous activities

I'm developing an Android 2.2 application.
I'm very new on Android, and I see that if Iaunch a new Intent from an activity, this activity goes to paused state.
If I want that user can't goes back to this previous activity, what must I do? May I kill this previous activity with finish?
UPDATE
An example:
A, B C and D are activities.
A is the first activity.
A launches B, and B launches C, and C launches D.
I want to close or kill activity B and C when D is launched.
Thanks.
You can use your intent to startActivity like normal and follow the startActivity with
finish();
If you do this on your hypothetical activity B, you can hit the hardware back button in C and it'll take you directly to A.
You might want to rework your design. What if you have A call startActivityForResult(B, 0), and when B would have launched C, instead setResult and finish. Back in A, onActivityResult gets called, with the result you set. You can use that result as a message for A to now start the activity C.
See if that helps - http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP (This and other flags)
And also you must invoke
finish();

Categories

Resources