I have developed an android apps that have a splash screen and a login page. The problem is when a user give credential and pressed log in button it open controlActivity. but when I pressed back button, I navigate to login page again If I pressed back button again I navigate to splash screen. How can I stop navigating to previous page, when user press back button then app should exit?
Why does it happen ?
This is happening because whenever you open another activity from intent, it creates it over it.
How to solve the problem?
You can add a flag like this
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
OR
You can finish it.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
finish();
If you want to close the app onBackPressed, you can override the method. Add this to your activity.
#Override
public void onBackPressed(){
finish();
System.exit(0);
}
Doing this, the app will be closed on back pressed!
You need to put a flag in the activity you want to be forgotten.
Intent intent = new Intent(this, Main2Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Answer by #Sambhav Khandelwal should solve your problem. In your splash screen you need to do add flags with intent like:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class); //assuming your splash screen name as SplashScreenActivity and login as LoginActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Or you can do without flags like below:
Intent intent = new Intent(SplashScreenActivity.this, LoginActivity.class);
startActivity(intent);
finish();
Related
Sorry if title is not clear, here's an in-depth explanation:
I'm in the Settings activity of my app and I have an option to change the app's theme. Obviously I want the entire theme of the app to change immediately when I change this option. So I'm using OnSharedPreferenceChangeListener in this way:
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals("theme_preference")) {
Intent refresh = new Intent(getApplicationContext(), SettingsActivity.class);
refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); //clears previous activities
startActivity(refresh);
}
}
As you can see I added flags that would clear the previous activities so that they'll be recreated too to have their themes updated.
The Settings activity recreates itself just fine, but then when I press the Up or Back button to go back to the previous activity the app crashes. Why is it crashing?
This is how you start a stack of activities:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
stackBuilder.addNextIntentWithParentStack(intent);
Intent nextIntent = ...;
stackBuilder.addNextIntent(nextIntent);
stackBuilder.startActivities();
I have this Activity1(Restaurant) which switch to Acticity2(Home)
Intent intent = new Intent(RestaurantActivity.this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
My other Activity (Home) has a boolean to check if I have been at Home before
public static boolean visitedHomeAlready = true;
if(!visitedHomeAlready) {
Intent intent = new Intent(HomeActivity.this, RestaurantActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); //
}
My idea is: If I have not visited the home before and switch to Restaurant Activity, I want to "remember" the data in the Home Activity to continue my game after come back home but it always start from the beginning from my home...
I am not sure which flags I have to set or which method to resume that state from where exactly where I switch the activity.
As Amin commented above,the finish(); means that the first activity has finished its job and the app will end it,if you removed it the second activity will start,but the first activity will still be in its place and you can go back to it just by clicking the back button on your phone or virtual device.
I'm using intent go back to the previous page and put finish(); But It goes like B page go in A page instead of B page back to A page. Do I type wrong code?
Here is the code
Intent intent = new Intent(Donation_Page.this, MainActivity.class);
startActivity(intent);
finish();
Change your code from
Intent intent = new Intent(Donation_Page.this, MainActivity.class);
startActivity(intent);
finish();
to
finish();
That's it
you can also try #Override onBackPressed to go back to previous Activity
#Override
public void onBackPressed() {
super.onBackPressed();
}
I have created a "Button" in android studio to send emails. When I click it, it sends an email as intended, but does not then return to the "Home" activity.
You can use finish(); method, but it's not recommended.
See: How to finish current activity in Android
http://developer.android.com/guide/components/processes-and-threads.html#Threads
But, this is my suggestion, you should be able to after clicking the Button, then clear the activity and restart it(or you can do something like Gmail App and doing some stuffs after clicking):
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
Also you can use:
Intent m = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(m);
Or:
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
Intent intent = new Intent(context, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
There are two flows in my app.
Flow 1:
Splash Screen --> Activity A --> Activity B --> Activity C --> Activity A
Flow 2:
Splash Screen --> Activity C --> Activity A
To explain the above; in Flow 1 a user sees the splash screen, goes to home screen, goes to login screen and on successful login, goes to main screen. If the user hits logout from the main screen he is redirected to the home screen.
In Flow 2 the user is an old user, he sees the splash screen and directly sees the main screen. If he logs out he should be directed to the home screen.
The problem which I am facing is that, in Flow 1, everything is working as expected. But in Flow 2, after logout (Activity C), the home screen (Activity A) is not opening. The intent is not working.
On click logout:
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
}
The Method:
public void logoutUser() {
//Problem: the intent is getting called in case the user creates an account. But, if the user is already an existing user, the intent is nit working
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
//logic to fix logout
// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
}
Why is the Intent not working in both the flows?
Try this way :
Intent intent = new Intent(StudyStoryMain.this, HomeActivity.Class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish(); // Call once you redirect to another activity
Before finishing current Activity start another Activity :
startActivity(i);
finish();
As in logoutUser() method you are calling finish() before startActivity() which is wrong as startActivity(intent) get never call.
So just change sequence to
startActivity(intent)
finish()
#Override
public void onClick(View v) {
((StudyStoryMain)getActivity()).logoutUser();
finish();
}
The Method:
public void logoutUser() {
ParseUser.getCurrentUser();
ParseUser.logOut();
Intent i = new Intent(StudyStoryMain.this, HomeActivity.class);
startActivity(i);
}
Try this way, hope it solves your problem.