How to prevent to relaunch specific Activity after pressed back button? - android

When I pressed the back button on B Activity to go back A Activity, B Activity is relaunched 'cause B Activity was launched from intent.extra at onCreate of A Activity.
The intent.extra is recreated with previous data, so it happens recursively.
My intention is to load B Activity from FCM and then back to the A Activity when the user pressed the back button.
I already tried to remove data of intent after startActivity but it didn't work.
But If I check savedInstanceState is null or not, it works but I'm not sure it's the right way or not.
Plus, the A Activity's launchMode is singleTop but it's not the point of this problem, I think.
Whole scenarios are the following.
Get the data from FCM in the background and then the data is served from the System tray.
Check if there is data or not at onCreate of A Activity and then if there is launch B Activity.
Pressed back button on B Activity but B Activity relaunched at onCreate of A Activity.
How to prevent the action of 3 in the scenarios?

Related

How to close an Activity when it's stopped?

Actually the scenario is a bit more complex than described it the title.
The situation is the following:
Activity A starts Activity B.
Activity A must not be destroyed when I start Activity B because I
need the user to be able to navigate back to A.
When the user presses the HOME button the user opens the Recent Apps
window and switches from my app to another app. At this stage both A
and B are STOPPED.
When the user user opens the Recent Apps window and switches back for
the other app to my app: Activity B is RESTARTED (activity A is not
restarted yet)
Now on Activity B there is a button to close the entire app, closing
both B and A, and it does close both activities using this approach:
https://stackoverflow.com/a/11509279/1815311
THE PROBLEM IS THAT WHEN ACTIVITY B TRIES TO CLOSE BOTH B AND A, IN
THE DESCRIBED SCENARIO, ACTIVITY A SOMETIMES IS NULL !!
How do I cope with such a scenario?
1 solution is - before finishing the activity B, store some variable with value 1 using sharedpreferences, finish activity B but not A. system will resume activity A. override onResume() function in activity A and get the variable from shared preferences, if it states 1 then store 0 there, and finish() activity A.
2 solution is overriding onresultactivity - see here
How to kill an application with all its activities?

How to save instance of an activity?

I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.

Android activity lifecycle - activity restarts when coming from history

I have a splash activity (A) that calls a listview activity (B) which calls another activity (C).
When I'm on activity C and I press Home, than kill the app (or wait of Android to do it), than longpress Home and come back to activity C there's a strange problem:
When I click back I go back to B. Than I have a backbutton handler that asks the user if they want to exit and calls finish() on the activity. When I try to exit in this scenario, activity A starts again.
On regular operation it finishes B and doesn't go back to A.
Why is that??
Thanks
When the app is killed (either by you or by Android) the process hosting your applications is killed. However, Android remembers the state of the activity stack (in your case A->B->C).
When the user returns to the app, Android creates a new process for the app and recreates only the activity that was at the top of the activity stack (in this case: C). Now the user presses BACK, which causes activity C to finish and Android recreates the instance of activity B which is then shown (You will see calls to B.onCreate(), B.onStart() and B.onResume()).
Now the user presses BACK again. Your back button handler tries to call finish() on activity A, but there is no instance of activity A. Android hasn't created it yet! When activity B finishes Android remembers that there was an instance of activity A in the activity stack underneath B so it recreates the instance of activity A which is then shown (You will see calls to A.onCreate(), A.onStart() and A.onResume()).
I hope this explains what you are seeing.
Make sure you are calling finish() on A when you load B

Resuming back to an activity without restarting the activity

am having a two activities that interact.
activity A picks input values and sends to activity B for the user to confirm input before submitting. activity B should allow the user to go back to activity A to edit input values if required or submit the values if they are ok. If user submits values, activity A should be finished and if he edits, then he goes back to activity A.
I have used startActivityForResult() and setResult() methods to kill the activity A when the user submits the values that has worked perfectly, but when i click the edit button to return to activity A, it call the activity A using a new Intent, and subsequently, it starts the activity A, yet previously started activity A is still running . what i want is to resume the activity A where it was left with the existing values before activity B started on click of the back button. How do i achieve this?? Your assistance is highly appreciated
Call finish() in Activity B when you need to edit. There is no need to fire an intent that creates new instance of Activity A. It will resume Activity A.

Is there any way to distinguish between an Android Activity onResume from the home screen?

Is there any way to tell whether an Activity is being resumed (i.e. onResume is called) from the home screen/launcher?
For example, if I have an Application with two activities, A and B.
Scenario 1:
Some user action on Activity A will invoke Activity B, bringing it into the foreground - moving Activity A into the background. As Activity A moves into the background, it goes through onPause() and onStop(). The user (now on Activity B) either finishes the Activity or hits the "back" button, bringing Activity A back to the foreground, causing an onRestart(), onStart(), onResume() sequence.
Scenario 2:
If the user hits the "home" button while Activity A is in the foreground and then re-invokes the Application from the launcher, it goes through the same lifecycle as in Scenario 1. I.e. User hits the "home" button. Activity goes through onPause() and onStop(). User launches the application again, causing Activity A go come back into the foreground, again going through the same onRestart(), onStart(), onResume() sequence as in Scenario 1.
As far as I can tell, the Activity has no way of knowing how it was resumed, it just knows it is being brought back into view. In fact, I have a feeling that there isn't really as much of a concept of an "Application" in Android - in the sense of something that has a single entry and exit point.
in Scenario 2, your activity will get an onNewIntent call, with the launcher intent passed to it.
You could capture the back button press on Activity B and pass an extra value to Activity A. If there is an extra value then the activity was resumed from pressing back on Activity B, if there is no extra value then the Activity was resumed from being hidden.
Could Acitivity A use startActivityForResult() to start Activity B and use onActivityResult() to detect that Activity B finished?
So the straightforward answer to the initial question is probably: no. Launching an activity from the home screen through an icon, or resuming it from the recents screen can not be observed from the intent it is (re)started/resumed with.
Depending on what you are trying to achieve there are some approaches though:
what #superfell suggested:
Check for whether the onNewIntent-method is called on your activity to decide if it was restarted from the launcher. As a precondition you need to set your activity to singleTask/singleTop launchMode in your Manifest:
android:launchMode="singleTask"
depending on what you're trying to achieve, this might be enough! But additionally you might have to deal with what happens when the user presses the back button. Default behavior is to finish & destroy your activity. Thus a brand new copy of it would be launched when it gets selected from the recents screen. Though onNewIntent would not be called, everything would be rebuilt from scratch. If you need to prevent this you can use:
override fun onBackPressed() {
moveTaskToBack(true)
}
finally when you navigate "back" from an activity you launched yourself onNewIntent will also not be called. If you further need to distinguish why your activity is resumed, you might want to start the 2nd activity with startActivityForResult so the onActivityResult is called when you get resumed.
Launcher activity & Intent extra
A completely different approach would be to have a "launcher" activity in your manifest that directly calls your "main" activity and finishes itself. When calling your main activity you can put an intent extra that allows your main activity to distinguish if it was just launched for the first time, or not. As the extra would be present on further onResumes, make sure to overwrite it the first time you "consume" it:
override fun onResume() {
super.onResume()
val firstLaunch = intent.getBooleanExtra(FIRST_LAUNCH, false)
intent.putExtra(FIRST_LAUNCH, false)
if (firstLaunch) {
// do something
}
}
and when starting from your "launcher" activity:
intent.putExtra(FIRST_LAUNCH, true)
startActivity(intent)

Categories

Resources