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.
Related
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();
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();
}
This question already has answers here:
Is it a good idea to call finish() after starting a new Activity in Android?
(3 answers)
Closed 5 years ago.
protected void onCreate(Bundle savedInstanceState) {
//if user is already logged in open the profile activity directly
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, Home.class));
}
buttonSignIn.setOnClickListener(this);
buttonSignUp.setOnClickListener(this);
}
Can someone please explain to me why is finish() called before launching the Home.class if the user is already logged in. I'm trying to go through some source code and not able to understand this.
simply when you call finish();
its more like an intent which do this :
Intent intent = new Intent(whereYouareActivity.this , mainActivity.class};
startActivity(intent);
when you finish an activity, the activity gets destroyed but not the process. This is an important aspect of Android programming and is important to understand how it's different from other platforms.
And you can finish your activity by adding these lines :-
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
After finish() you can't get context from the activity.
You have to start activity like this:
if (SharedPrefManager.getInstance(this).isLoggedIn()) {
startActivity(new Intent(this, Home.class));
finish();
}
By calling finish() you actively close your current Activity. This prevents your activity from being shown again when the user presses the back button.
I need to build a fake back stack in my application. I am starting an activity using aContext.startActivity(aIntent) and would like to build a stack to allow the user to go to the HomeActivity instead of exiting the application.
Now I know about the TaskStackBuilder but I am not sure on how to implement it when it comes to startActivity method.
This is what I got so far when building the stack but I am not sure how to use it in startActivity method
Intent detailActivity = new Intent(aContext, DetailsActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(aContext);
stackBuilder.addNextIntentWithParentStack(detailActivity);
PendingIntent pendingIntent = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Why don't you just use onBackPressed
Context context = this;
#Override
public void onBackPressed()
{
Intent intent = new Intent(context,YourActivity.class);
startActivity(intent);
}
You need to start activity without adding to back stack, then override the onBackpressed
//Start activity without adding to history stack
Intent i = new Intent(...); // Your list's Intent
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY); // Adds the FLAG_ACTIVITY_NO_HISTORY flag
startActivity(i);
//Override the newly open activity onBackPressed()
#Override
public void onBackPressed()
{
Intent i= new Intent(getApplicationContext(),HomeActivity.class);
startActivity(i);
}
The fix that i found to work without having to use onBackPressed was to add a flag to the intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
For building fake stack use TaskStackBuilder.This will help you to create back stack.
Suppose you activities are 'ActivityA', ActivityB, ActivityC, ActivityD. Now you are in ActivityB and from ActivityB you want launch ActivityD and want a back stack like this:
ActivityA -> ActivityC -> ActivityD
That means back press from ActivityD will go to ActivityC and back press from ActivityC wil go to ActivityA
In ActivityB you can use this
TaskStackBuilder.create(this)
.addParentStack(ActivityA.class)
.addNextIntent(new Intent(this, ActivityA.class))
.addNextIntent(new Intent(this, ActivityC.class))
.addNextIntent(new Intent(this, ActivityD.class))
.startActivities();
To use TaskStackBuilder you min sdk version should be 16 or higher.
For sdk less than 16 you can manually check in onBackPressed and start Activity.
In Activity D:
#Override
public void onBackPressed(){
Bundle extras = getIntent().getExtras();
if (extras.containsKey("FROM_ACTIVITY_B_FOR_STACK")){
// start Activity C
}else{
super.onBackPressed();
}
}
Do this for other activities where you want to add back stack