I have two activities, one is MainActivity (the launch activity), the other is MaskActivity. If I start my app from other app, and start MaskActivity from MainActivity (now MaskActivity is on top of MainActivity), problem arises when I press home button on the phone and re-enter my app, a new MainActivity is started and put on top of the MaskActivity and the old MainActivity, which is not what I want.
But if I start my app from program list(not from other apps), things act quite right as I wanted, when re-enter my app, no new MainActivity is started and MaskActivity is on top of MainActivity.
My MainActivity's launchMode is "singleTop", I don't know if this is the problem. Simply change it to "singleInstance" or others can not fix the problem. When changed to "SingleInstance", it won't start a new MainActivity but will bring MainActivity to top. But I want the MaskActivity stay on top when the program resumed if the MaskActivity is on top of the MainActivity before the program paused.
Any ideas?
What you're asking for is a little unusual. Usually apps that provide intent-filters have specific activities that can handle those intent filters. For example, if you have a image viewing application, the "image gallery" activity will handle the "view" intent-filter.
If I understand correctly, you're saying that both activities can handle the case where your app is launched from another activity? Perhaps your two activities are closely linked (in terms of functionality). Perhaps you should have just one Activity, and put the view content of the two Activities into Fragments. Those fragments can be hidden or shown within your Activity. Your Activity can be singleTask, and will always be showing the correct content when it is launched (as you want).
Related
I have an app with MainActivity.java which has a button that when clicked launches Main2Activity.java. Main2Activity.java is added to the project in Android Studio using New/Activity/Basic Activity. It has a back arrow at the top and touching it will return the app to MainActivity. AndroidManifest.xml has the following for Main2Activity:
android:parentActivityName=".MainActivity"
The app also has a BroadcastReceiver which, after performing its work, launches Main2Activity.java. When started this way (when the app is in the background), the back arrow in Main2Activity does not go back to MainActivity but instead exits the app.
I would like the back function of Main2Activity to always go to MainActivity. According to Navigate up with a new back stack, there seems a way to do this, but the example is meant for an activity launched by another app.
How do I set the back destination for an activity launced by a BroadcastReceiver?
There are two ways of doing this:
Providing the Up Navigation as mentioned in the link quoted by you in the question. It should work even when you're navigating to the Activity through a broadcast.
Use startActivities from the BroadcastReceiver to start both MainActivity and Main2Activity. This allows you to create a backstack when starting an Activity and it's parent is not on the stack.
I prefer approach 2 simply because it works even when you press the hardware back, not just the back button on the Action Bar. For achieving that with approach 1, you'd also have to override onBackPressed.
What are Some application examples, or use-cases where (singleInstance, singleTask, singleTop) serves a necessary purpose. i.e. why would one favor one launchMode over the other? so far my experience with them is strictly notes, so it would be helpful to understand where they're used. thank you!
I understand it is known that it means you cannot launch multiple instances of an activity.
In my example, I use singleInstance in my main launcher Activity, because it has Fragments, and it is being launched by notification intent.
If Activity was sent to background after user touched "home" button, I don't want it to be launched from background via notification intent, because it will show the last seen fragment. If I set singleInstance, it will always launch new instance of activity, and show the main fragment.
In my case I can't use singleTask because it holds other activities from my app in the stack, but puts the main activity on top. I don't need that history in the stack.
singleTop launches new instance on an activity only if it is not in top of the stack. if it is on top, it launches from background, that's what I don't need in my app.
Hope I was clear :)
I have an Android app with multiple activities. The main activity communicates over a network and can launch or dismiss various other activities depending on commands it receives over the network. When an Activity is dismissed I don't want to finish() it, just move it down the stack so it's no longer the top activity. What I really need is a FLAG_ACTIVITY_REORDER_TO_BOTTOM but there is no such thing.
There's an intent flag called FLAG_ACTIVITY_PREVIOUS_IS_TOP and the name implies something like that but I don't understand the description:
"If set and this intent is being used to launch a new activity from an
existing one, the current activity will not be counted as the top
activity for deciding whether the new intent should be delivered to
the top instead of starting a new one. The previous activity will be
used as the top, with the assumption being that the current activity
will finish itself immediately"
Could someone please decode that for me, and if it's not what I want IS there some way to tell an activity to submerge itself below the previous one?
This isn't possible. The activities are stacked and you cant put one back under the other. It sounds like you may want to create a class that extends Android’s android.app.Application.
I found this tutorial online and it looks good. Good luck.
Extending Android's android.app.Application tutorial
You cannot move an activity below a certain activity into the android back Stack. The only way to move a activity in back stack is to open another activity on top of it. You can move an activity on top by creating a single instance of activity using FLAG 'singleTop' in this way your activity will be moved to the top of another activity and only a single instance of activity will be there in stack.
More information about activity back stack and Flags is available here.
Go through this information and all your doubts will get cleared about back stack.
I am trying to create an application that defines two activites. The first activity pretty much runs all of the time. The second activity requires the user to authenticate to use the device.
Most of the time this application works fine. However, I am having problems figuring out how to force the second activity to the top of the window stack. The code calls startActivity passing in an intent to start the second activity. The problem is that when another application is running (e.g. a web browser), the second activity is not on top. When the other application exits, the second activity is visible to take input from the user.
Here is the activity definition for the second activity that I want to have always on top when started:
<activity android:name=".Authenticate"
android:launchMode="singleTop"
android:configChanges="orientation|keyboardHidden">
</activity>
It is my understanding that when start activity is called, it will put the new activity on top. Is there something that I am missing? How can I make the authentication activity come to the top when it is started?
Well AFAIK, you cannot force your Activity to stay on top all the time. If some other process (say Web Browser) creates an Activity after you have created yours, then that processes Activity will have focus and not yours.
whenever a new activity starts it is on top of the stack and if another application is running then it will be on the top of the stack and if you want your activity on top of the stack then you should stop other application being launched....Is there something I am missing then let me know..
My Application have 20 activities. Here i want to implement the how to exit from the application when you click on the button(like Logout). it means if you click on the menu button any where of our application then it shows the one button. if click on that then directly comes out from the application. how to implement it.
thanks
Well naresh you can do something like that
first finish the activity from which you are closing application this.finish(); secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this clear top when you switch from one activity to another activity and as you know every activity is kept in stack to so this flag remove old activity from top and push new activity in top so around your whole application only one activity is kept in stack
and if this does not work put the whole application in background by avtivityname.moveTaskToBack(); this will move your whole app in back ground but only one drawback when you start your activity it will show your that activity from which you have moved back
System.exit(0);
should work, don't forget Java common functions work on android, there isn't only the android library!
As for the button being in the menu in every activity, you could create a class derived from Activity which creates and handles the menu properly, and make every other activity inherit that derived activity.
First finish the activity from which you are closing application: this.finish();. Secondly and most impotantly always set a flag i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); This clears top when you switch from one activity to another activity. As you know every activity is kept in a stack to so this flag removes old activity from the top and pushes new activity to the top so around your whole application only one activity is kept in the stack.
If this does not work, put the whole application in background with avtivityname.moveTaskToBack();. This will move your whole app to the background. One drawback: when you start your activity it will show your that activity from which you have moved back.