Previous activity stays on activity stack - android

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()

Related

How could I close all opened activities other than MainActivity

When my activity stack is like,
MainActivtiy -> Activity1 -> Activity2 -> Activity3
I need to go 3 back states to reach MainActivtiy from Activity3
I could be able to close opened activities from Activity3 like,
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
But, above code segment closes all activities including MainActivtiy and starts new MainActivtiy. I want to know are there any other options to do the task that I need. I dont want to create new Intent because it drops the data of static methods that I have created in my MainActivtiy.
Please help me, Thanks in advance.
You should use FLAG_ACTIVITY_CLEAR_TOP which will bring the running activity on top and remove all other activities above it
Note : it will trigger the onNewIntent of already running activity otherwise mention launchMode = "singleTask" in activity tag, inside manifest

Clear Android activity stack and start activity

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.

How to logout after killing all activities?

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>

Clearing android stack before starting a new 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.

Start fresh application every time

Right now, what is happening is when I press home-key, last active activity is displayed. But what I want is to start the application as new every time the user presses home key and start the app from launcher. Please help
I think you can override onPause() event when you pressing home button and in there you can implement finishing your activity.
with setting intent flag as FLAG_ACTIVITY_CLEAR_TOP on starting your Activity your back stack would be clear so with finishing the last activity there is no activity on the task and stack
While creating intent you can set the flag. for example
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

Categories

Resources