I have an App that has a Main (root) activity which is a splash screen and launches a menu activity through an intent. The problem is when I am in the menu and press the back button it goes back to my splash screen as it should, but when I press the back button again to exit the app it closes my splash screen activity and goes back to the menu activity. At this point I can press the back button a third time which then appears to fully close the app. I believe the solution may to to use some launch mode configuration. Any ideas?
Does the splash screen automatically start the Menu Actvity? If it does you may want to start the MenuActivity by using startActivityForResult(Intent, int). Then in the Menu activity override onBackPressed and pass back a code letting the Main Activity know to finish(); and handle it using onActivityResult(int requestCode, int resultCode, Intent data)
This link on the Android Docs should help.
Related
I have a login Activity and Home Activity in my app. The login Activity is the launcher Activity and it checks if user is already logged in and then auto directs the user to the Home Activity if he is already logged in. Now when the user is in the Home Activity and presses the back button, I want the user to quit the app like the same behavior that occurs when back button is pressed on the launcher activity. How do i achieve this? I definitely don't want him to be taken to the login activity because he is logged in already.
What I am doing right now is this, I have added the 'clear top' intent flags but user goes back to the Login Activity with this:
val intent = Intent(AuthUI.getApplicationContext(),
HomeActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
Also, I am not using the Navigation Library in android
You can finish the Login activity after starting the Home Activity.
startActivity('intent that starts home activity')
finish()
Or
you can call the finishAffinity() on backpressed in the home activity
I guess you'll be using an Explicit Intent to launch the HomeActivity after verification. Hence you can just call finish on the LoginActivity immediately after you start the Intent that launches your HomeActivity.
i.e
startActivity('intent that starts home activity')
finish()
Then when the user presses the back btn on the HomeActivity, they'll exit the app.
You have a couple choices there:
You could finish() your login activity, so that it gets removed from the activity back stack.
You could use fragments for these screens and not add the previous one to the backStack while replacing fragments.
You can override the onBackPressed method to see if the user is still logged in, so that you can route them to the correct activity.
You could think of having a launcher activity which is a dummy activity whose onCreate() opens either the login activity or the home activity based on user login state, so that the login activity does not open in case he is logged in already, but this would still have the same problem of having you to finish() this activity before moving to the next one.
I have two activites. Activity A and Activity B.
Activity A is a singleTask activity. Activity B has no extra configuration. So it is a normal activity.
When Activity A launches the activity B then if user presses to the home button then comes back immediately (so my app is not killed by android yet.) My app is showing activity A. But it should show the activity B because the user pressed to home button when active activity is B.
If i remove the singleTask property from Activity A. It is working as expected. But i have to use singleTask activity for some other reason.
How can i resolve this problem?
As you have constraint to use singleTask launch mode, you can do following :
When user press Home, app or Activity B goes in background (stopped state)
Then when you open app, it'll show Activity A because of singleTask launch mode
In Activity A, override onNewIntent() method. This will get called from android os. You can fire Activity B's intent from this method. So, system will open Activity A when coming from background to your app, but from this method, Activity B would be shown.
I've edited my answer to handle below scenario :
If we're on ActivityA, we press Home button and we come back, ActivityA should be shown, not ActivityB. To do this, I used boolean shouldOpenB
in Activity A :
static boolean shouldOpenB = false;
// set shouldOpenB to true when you are opening ActivityB from your code with Intent
// reset shouldOpenB to false in onStart() of AcrivityA
protected void onNewIntent (Intent intent) {
if(shouldOpenB) {
Intent openIntentB = new Intent(this, ActivityB.class);
startActivity(openIntentB);
}
}
Note : The animation of Activity A -> Activity B would be visible to user for fraction of time. If you agree to change to singleTop, we can remove this animation also.
When user presses a back hardware button in my activity he will either be taken to a previous activity or to the home screen (depends on how he got into that activity in a first place). Is there a way to determine where exactly user will be taken inside of onBackPressed method?
In your home and previous activity when you start the intent to go to this activity, you can add an extra to the Intent.
Intent.setExtra("PREVIOUS_ACTIVITY ", "HOME")
Then you can check in your activity onBackPressed() if it came from the home or another activity.
My Android app is for a hiking club and starts with a Home screen, which is the main activity.
The home screen shows the house-style and has some buttons to the core functionalities: list of upcoming hikes, history of hikes, last reactions and also a message board.
THe activity flow is pretty straightforward, you can i.e. navigate from:
Home -> Hike list -> Hike Details
And back using the Back button. Going back from the Home activity will ask for closing the app.
I already use the FLAG_ACTIVITY_CLEAR_TOP flag to prevent several instance of the same activity.
But my problem is I have also implemented a Menu to navigate to the core functionalities directly.
So for example, when in the Hike Detail screen, one can choose to go to the Message board. But I do not want to keep the Hike List -> Hike Detail activities on the stack.
So when pressing Back from the message board, I always want to return to the Home activity.
IS there a clean possibility to pop the stack and only keep the Home activity before launching a new activity? I guess that would solve my issue.
I found something similar on SO that might work.
Check out: FLAG_ACTIVITY_TASK_ON_HOME
Pre API 11:
Start all of your activities from home using startActivityForResult(). When you navigate to a parallel stack via the global menu. Call this on your current activity:
// startParallelActivity();
setResult(KILL_YOURSELF);
finish;
Where every activity on top of home implements onActivityResult() like so:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == KILL_YOURSELF) {
setResult(KILL_YOURSELF);
finish();
}
}
This will destroy all the activities in the current stack, leaving just the home activity that will be there when the user hits "back"
I have two activities A and B. What I want is to show activity A as an intro, the user will be unable to navigate back to it. Is there some flag I can set to activity A to do this? Can I block the back button for one activity only? Activity A is of course my main activity which automatically starts activity B after some "hard work".
Best regards.
you do not need to block the back button, but just call finish() on your A activity after firing an intent to start B. Back button pops the previous activity from activity stack and it won't be able to pop A if it is already finished.
For this you don't need to block the Back button. Simply, start the second Activity and quit the first one. And now if user presses the Back, they will be taken to the Android home screen not on your apps home screen.
Updates: By the way if you want to intercept the Back button for any reason, simply override the onBackPressed() method of Activity class. See this for details.
Never override the functionality of a hardware button.
You should call finish() in Activity A right after starting Activity B (calling the Intent).
it works but the application terminates and i'm redirected to android's applications screen. I would like to stay in activity B if back button is pressed, i don't want to exit the app. here's is what i got :
public void startProgram(Context context){
Intent intent = new Intent(context, ActivityB.class);
startActivity(intent);
finish();
}