I have 3 activities with sequence A->B->C.
A is the Login Activity.
B is the Welcome Activity
Now When I start the app, I'm at Screen B, because I was logged in.
Now when I press back it takes me to Screen A, that I dont't want.I want to close my app after back press but with condition When I press logged out on Screen B, then I should be at Screen A.How to resolve this?
You can do this many ways . one simple solution is .
Finish activity A when moving to B , So now you press Back button on B the app will close.
startActivity(new Intent(SplashActivity.this, A.class));
finish();
On Logout launch the activity A.
Intent intent = new Intent(this, A.class);
// Flag are optional if you have multiple activities in stack
//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
Related
I know this question has been answered in another posts also. But it doesn't solve my issue.
I have 3 screens: A,B and Home. App starts with A, then goes to B and then to Home page. But I want to delete activities A and B when it reaches Home activity, so that when back is pressed app exits.
I tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP. It works fine when back is pressed. App quits. But if I again open the app from background, it starts with screen B.
Intent.FLAG_ACTIVITY_NEW_TASK. This works exactly as I want. But when Home activity starts, there is a sudden glitch in the screen and it is not smooth.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Try the following code:
Intent i = new Intent(YourActivity.this, HomeActivity.class);
i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
You can do this way also, for your splashScreen(A) you can set nohistory=true in AndroidMenifest.xml, and goto Activity(B), and when you are going on Activity Home from Activity(B) finish activity(B) than goto Home activity.
so both of your previous activities will not store in stack.
And on Activity B override onBackPresh() method but remove super.backpresh() in side it. and start your activity A with finish this current activity, it will work.
I'm using this library which is a Wizard.
When user is done with the Wizard and clicks Done button he moves to another Activity.
I have added to Manifest file android:noHistory="true" for the WizardActivity and also when I start the next activity im doing it like this :
Intent intent = new Intent(this, ProjectsActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Nevertheless when user click backs he still goes to the WizardActivity. I'd like the user either to exit or go the activity that started the WizardActivity.
Thank you
finish() the activity after launching the other Activity. i.e after startActivity(intent) add finish()
On my App, I have the following process to log on a user:
The user enters its credentials on the MainActivity and is redirected to HomeActivity using that code for navigation:
Intent accueilIntent = new Intent(getApplicationContext(), HomeActivity.class);
accueilIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
finish();
startActivity(accueilIntent);
When the user is on the HomeActivity, he is able to log out thanks to a log out button. Since the event is fired, I would like to kill all activities and redirect the user to the login activity (MainActivity). So The code which is fired on the event is the following:
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
The user is well redirected to the MainPage. Then when I press the home button to get back on the Android home screen and then I come back to the App, the right MainActivity appears. However, when I press the native back button to return to the android home screen and then I come back to the App, the HomeActivity appears (the one that should be destroyed before) and not the MainActivity.
Does anyone can tell me why I'm coming back to HomeActivity and not MainActivity in this case?
1] add finish() before startActivity()
2] Add FLAG_ACTIVITY_NO_HISTORY flag.
Intent intent = new Intent(mContext, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
If you want user to come back to MainAcitvity when he presses back button on HomeActivity, the parent of HomeActivity should be MainActivity.
In your manifest.xml file, in HomeActivity's tag add this :
<activity
android:name=".HomeActivity"
android:parentActivityName=".MainActivity" >
....
</activity>
I have an application with three activities: Login, Register and Main activity.
I want to start the Main activity after a user had logged in or registered.
But, when the user presses the 'back' button, he should not see the activity from which he logged in. In other words, I want to clear the activity stack before starting the Main activity.
I saw a few solutions online, for example: this, this and this.
They did not help.
This is my code for launching the main activity from the Login activity:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
The code in the register activity is quite similar.
I also tried to put finish(); after I started the activity, but it only closed the current activity. Meaning, if I started in the login activity, moved to the register activity and logged in there (Login -> Register -> Main), When I will press the 'back' button I will be return to the Login activity.
Thank you for your help!
Yuval.
The easy way would be to use "noHistory" attribute in manifest. You can set it for the login activity in your manifest file
<activity
android:noHistory="true"
/>
You can do it like this:
If the user goes to the registration screen then just finish the login activity and when they click the back button on their device you can call the onBackPressed () method. In there you can finish the registration screen and start the login activity. If the user went to the registration screen and then registers, you also want to finish the register activity and open the main menu or something else.
Login:
Click on register? Start register activity and finish the login
startActivity (new Intent (Login.this, Register.class));
finish ();
Register:
Click on submit? Start main activity and finish register activity
startActivity (new Intent (Register.this, Main.class));
finish ();
Click om back button?
#Override
public void onBackPressed (){
startActivity (new Intent (Register.this, Login.class));
finish ();
}
And you can also use in the androidmanifest:
android:noHistory="true"
In the activities you want to have no history (where the user can't go to if they click on the back button).
But I prefer to do it in code!
Hope this helps you!
Try this:
Intent intent = new Intent(this, SomeOtherClass.class);
// do not keep this intent in history
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
What you are doing is almost correct, just make a small change in the Intent.
In place of using,
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
use:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
getApplicationContext().startActivity(intent);
It will clear the top, start a new task and clear all the previous task as well. Now, all that Android knows is that there exists a MainActivity in your stack. You can write:
#Override
public void onBackPressed() {
finish();
}
in MainActivity which will allow the user to exit the application and not go back to the LoginActivity.
I have A and B activities. A is singleTop on launch, B is excludeFromRecents. When I back from B to A, B is always destroying. How to prevent it? When I press Home then B is just stopped and not destoying. Thanks.
Following back code if B activity:
Intent intent = new Intent(this, ActivityPlaylist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Without flags back not working at all.
I also have prevention of creating new instance in onCreate of A reason of I launch app from Eclipse (bug):
if (!isTaskRoot()) {
// Android launched another instance of the root activity into an existing task
// so just quietly finish and go away, dropping the user back into the activity
// at the top of the stack (ie: the last state of this task)
finish();
return;
}
Upd: my case is I need do not lose B condition, including list position and so on.