My app main launcher is the LoginActivity which, in most of the cases, at the end start MainActivity from where all actions in my app can be launched
This is the "normal" scenario
however, when someone lauches the app from share context, or notification the LoginActivity redirects him straight to the activity responsible for handling that specific scenario...
this way when the user presses "back" on cellphone the activity stack goes back to login, instead of main
How can i tell login activity to put two activities on stack in order to create this "fake" back history
Take a look at https://developer.android.com/reference/android/support/v4/app/TaskStackBuilder . It will help you create the back stack you are looking for.
Related
I basically try to mix these two tutorials:
deep linking from notification with back stack
deep linking from url
What I want to do is to launch a specific activity of my app when the user click on a url in an email. When the activity is launched I want the user to be able to press back and to go to the parent activity like if the user had land on this page following the normal journey throw the app.
I know how to open the specific activity but I don't know how to generate the backstack. In the example they are able to manage the backstack as the deep linking comes from a notification generated by the app itself, so they can create a pending intent. In my case the link is from an email so I can't do that.
Any idea?
Create activity without content view for handling incoming intents. This RouterActivity handles intents and decides what activities should be started with startActivities (TaskStackBuilder) after that it finishes itself with finish().
RouterActivity should use theme: #android:style/Theme.NoDisplay (use Activity instead of AppCompatActivity) and should not set any content view
so user will have no clue that there was any activity started before desired one.
The main activity that starts when my app launches is shown with a bunch of menu items but access to some of the activities that are launched by some of the menu items require a username and password. After the user enters their credentials correctly, they have access to those activities. They can also use the back button and return to the main activity where the menu is located and I don't require them to re-enter their credentials again, as long as they remain within the app.
If however they hit the Back button while in the main activity, I terminate the app with finish();
The problem I am seeing is that if the user presses and holds the Home button, the list of apps is shown and if they tap on my app, they could end up being taken to one of the "secured" activities that requires a username and password. This can happen if they were using one of those activities and hit the Home button.
The solution I came up with is just to use a global variable that indicates that they are signed in and clear this when they hit the Back button from within the main activity.
But I need to check for this flag in the onResume of every activity to see whether they are signed in or not and if not, I do a finish() in the onResume preventing them from accessing the activity.
Is there a better way of doing this? Perhaps a way of terminating all the activities that are spawned when the user enters a "secured" activity? Or better yet, terminate all activities within my app? As it stands, I have a lot of activities and repeating the code in my onResume seems kind of senseless.
Can anyone tell me what the difference is between opening an app from the applications screen and opening it from that recently used apps list that pops up when you long-press the home button?
I didn't even know that recently used list existed until a friend managed to break my app by launching it from there. He tried twice and got the same force quit, but when he launched it from the applications screen it opened fine.
The error log told me that a nullPointerException occurred in the getCount method on my ArrayAdaptor for my ListView.
Anyway I just wondered if there was a difference that I need to know about and adapt my code to deal with?
AFAIK, If your application is completely shutted down, launch from applications screen and recently used apps list should have no difference, both refresh start your application and open your application's MainActivity (by stack-push your application's MainActivity into a newly created task)
However, as Android is multi-task OS, your application can be put into background in standby mode i.e. open your application then short-press home button, this is not same as press back button. If you haven't override these key pressed in your application, press back button several times with pop all your activities off from activity stack and finally kill your application, whereas press home button will bring System's HomeActivity into foreground hence flip your application (AKA. task with activity stack) into background.
Things becomes more interesting here, depend on which value your configure your activity's android:launchMode in AndroidManifest.xml, if you use standard or singleTop:
1. launch app from recently used apps list always bring your standby activity back to foreground, i.e. re-order activity stack.
2. launch app from applications screen will create a new instance of your MainActivity and open it, i.e. push a newly created MainActivity into activity stack, so now you have two instances in your application's activity stack
If you use singleTask or singleInstance:
2. launch app from applications screen will use the standby MainActivity (if exist) in your application's activity stack and re-open it, i.e. re-order activity stack.
Checkout Tasks and Back Stack to see how different configurations may affect your application's activity stack behaviour.
I believe there should be no difference. These are the lifecycle methods I typically see when pressing the home button from an activity, on android 2.3.4
onPause
onStop
then when I use either the icon or previous applications to navigate back, I see
onRestart
onStart
onResume
Now, in some cases the system will tell your activity to finish while you are away (or immediately when you return if an orientation change occurred). Then you will see onDestroy, and the following when you navigate back
onCreate
onStart
onResume
I don't think there is anything mysterious going on here. According to the Activity documentation, there are only four states that a process can be in, and both of these fall under background activity.
There shouldn't be any difference in how the activity is launched from history, apart from the fact that the launching Intent will have the FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY set.
Here's an easy way to think about it. All of your activities are launched form Intents. Holding down the home button allows you to open that activity using the last intent that launched it. This can give you some unexpected results however. For instance, if you are able to launch your activity from something special like a widget.
When I launch a notification from the "second page" of my app and then close (hide) the app by going to my home screen, I can get directly back to the second page by clicking the notification. However, pressing back from this point should, I think, take me back to page one, but it instead closes the app as if it's on the first page. Am I missing a flag of some type?
Thanks
When you start activities they stack on top of each other giving you a history stack, when you start an activity directly there is no history.
Not sure if there is a super easy way to do this but people have rolled their own but it really seems like their should be a better way to do this ...
I have an activity which is declared in my manifest with android:noHistory="true", and 90% of the time, this is the desired functionality. This particular activity doesn't need to be saved in the application's history, and the application's UI flow is greatly complicated when it is in the history and needs to be manually removed by calling finish() every time the activity would otherwise be added to the stack.
The problem is, that this activity has the ability for the user to send an email, which is of course accomplished by creating an Intent with the ACTION_SEND property. When the user presses the "back" button from this activity, the next one to be shown in the stack is actually the one underneath my activity. Is there any way that I can force Android to add my activity in the history stack before presenting the ACTION_SEND intent?
I look forward to be proven wrong but I believe that the history stack is updated when the activity is created. At first I would suggest the rather hackish solution of launching a "proxy" intent that would be pushed onto the stack which could then call the e-mail intent and would be smart enough to launch the proper intent on the way back.
I'll update my answer if I find something in my notes.