Start back an activity that's been killed with finish() function? - android

After user logs in and goes to second screen(activity), I killed the Login screen activity using finish(). Now that I'am on second screen and I have Logout button there in the action bar, is there a way that login screen which has been killed can be brought back on log out button press ??

You just need to start it again.
startActivity(new Intent(CONTEXT, LoginActivity.class));

Intent intent = new Intent(SecondActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
use this some line when you logout from second activity..

Related

How to skip an activity and close app

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

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.

Other activity is also brought to front on StartActivity

I have two activities (A and B) in my app and some BroadcastReceiver.
I encounter the following scenario:
A is running and was closed using the home button (onStop was called).
Some time after, BroadcastReceiver was triggered with some intent. It run the following code:
Intent activityIntent = new Intent(context,
B.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(activityIntent);
And B is indeed started, however A is also brought to front (behind B). How could I avoid A being fronted?
when you are pressing home button you are not closing the app actually(i mean it's in pause state) and it's in back stack and whenever you started a new activity of same app and close that activity it pops out the top activity in back stack.. So if you don't need this thing happen then please try following code
#Override
public void onStop() {
if(!isFinishing())
finish();
super.onStop();
}
From BoardcastReceiver you can start Activity B using this intent filter. This will clear the activity stack and pop Activity A from the stack.
Intent intent = new Intent(context,B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);

Android close all activity when sing out

In my app have a sign in activity. If login is successful, I start the first activity. It includes the menu, which opens ActivityA, ActivityB, ActivityC... and from these activities I can go deeply ActivityASubA and deeper ActivityASubB...
-ActivityA
--ActivityASubA
--ActivityASubB
-ActivityB
--ActivityBSubA
--ActivityBSubB
...
And if the user open ActivityA and ActivityASubA and later from the menu ActivityB and after that sign out, I open the sign in activity, but in the history will be the previous activities and if he press back see again these activities and it's a problem.
So when sign out I need to close all activity. Which is the best solution?
In sign out method you should put this code:
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(new Intent(this, Home.class));
finish();
About FLAG_ACTIVITY_CLEAR_TOP you can read th

Categories

Resources