Activities and their lifecycle - java

Consider this:
Activity A
Activity B
Activity C
Activity A is started when clicking on launcher icon. Activity B is started by activity A - nothing special. But Activity C should be started ONLY by app itself (not on click or something similar ), like broadcast.
THE PROBLEM:
When Activity C is started by app automatically, and when i press HOME button on this activity, and when i click on lanuncher icon (to show up Main Activity - A ), it shows me activity C. If i press BACK button on this activity, then i can go to activity A, with no problem.
How to solve that issue? Killing activity C, on HOME button press, or is some other way to do this?

In manifest of your Activity A put android:launchMode="singleTask" android:clearTaskOnLaunch="true" and in rest of the Activities put android:clearTaskOnLaunch="false", This will solve your problem. Hope this will help you.

Just add android:noHistory="true" to <activity> element of Activity C in your AndroidManifest.xml.
Setting true for android:noHistory will make the activity not leaving a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

Related

Navigate & finish multiple instances of same activity on button click

This one is weird. I'm implementing breadcrumb navigation on a older app and I'm not at the point where it's ready to refactor the views. Unfortunately every new navigation opens a new activity and sometimes the new instances of the same activity. So my breadcrumb navigations basically look like this:
Activity A > Activity B > Activity C > Activity C > Activity C > Activity C > Activity D
So if I'm on Activity C and I need to go back to Activity B I can just set the Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP for the intent and navigate back no problem. But if I need to navigation from the 4 instance of Activity C to the 2nd instance of Activity C I'm not able to use any intent flags to make that happen. My app is already making use of the back button so I cannot override that to utilize here. Does anyone have any ideas as to navigation among the multiple instance of Activity C?
Fortunately all these app do inherit from the same parent app which extends FragmentActivity if that is helpful.
To go back one item in your breadcrumb you don't have to use the BACK button, you can just call finish(), which will finish the current Activity and go back to the previous one in the stack.
To go back to a specific Activity, you can use startActivity() with flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOP, as you have already indicated in your question.
Anything else is not possible. See my answer to a related question here:
https://stackoverflow.com/a/39104837/769265

Return to Main Activity after TAG_DISCOVER

I finally got my app reacting to TAG. Its build from 2 different activities.
activity A is the Main activity, that get launched when i start the app.
And then there is activity B which is being launched by a TAG_DISCOVER.
However i have 2 problems now.
(1)
Activity B has a backButton, which just does finish(); If the app were manually started i would just return to activity A, as its being startet first. However if activity B is directly launched through a discovered TAG, A isnt launched, so when pushing the BackButton the App exits, which i dont want.
(2)
Each time I discover a TAG Activity B gets launched, eventhough it might already exist. But I want Activity B to only be active once a time.
This is a similar question to this
You should be able to use the same approach - detect that the activity was launched from a Tag and go 'back' to the main activity.

Using EditText in Activity Group

In my application i am using tabhost. And to control different activities i am using activity group. I have Activity A from which i am going to activity B. Activity B contains Edit Text, Spinner, Button etc. Now when i scroll the Activity B and press the device Back Button than it does not go to the previous Activity. It goes out of application.
Kindly suggest the answer.
This is the way the Activity lifecycle works for Activity Group. Try using fragments instead of activity group with your tabhost to get your movements. Also, please edit your question title to more accurately reflect the actual question.

Android avoid OnResume

I have 3 Acivity based application, It's work flow like this MainActivity ListView and DetailView. when onResume event trigger , need to call MainActivity. without going to other two activity.
Is there any way to call MainActivity when onResume event trigger?
Thank You
You can set the android:clearTaskOnLaunch="true" attribute for you MainActivity in the AndroidManifest.xml file. See here to find why and more details. I think this is the most convenient way to meet your demand.
Edit:
I just tested and found this only works when you exit the app and launch the app from the app drawer(NOT long press on HOME and select the app).
If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare "android:launchMode="singleTask" for the root activity, here, the MainActivity.
The best solution I can think of is to start the activity again in the onResume of all your other activities:
#Override
public void onResume() {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
The user will still be able to hit the back button and go back to the previous activity, however.
If you want to quit your List/Details views when the user closes your app, have them finish() themselves in their onPause which is called when your Activity is closed.
The only caveat here is that calling finish() will move it one Activity back in the ActivityStack so if your MainActivity isn't the one launching the List/Details views, it will not go back to the MainActivity. In this case, you could specify in the AndroidManifest.xml
<activity android:name="sample.activity.MyActivity" android:noHistory="true" />
to prevent the List/Details activities from ever being put into the history.

Android: launchMode=SingleTask problem

I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html

Categories