Clearing Activity Stack on Intent Start - java

I have an app that I'm writing that starts with an initial Login/Create New Account splash screen. In onCreate it checks my SharedPreferences to see if user credentials are stored. If they are the activity launches an intent to the main activity skipping the login/create process.
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
I don't want the user to be able to go back to the splash screen. As you can see I'm trying the CLEAR_TOP Intent flag as suggested by this but it doesn't seem to be working. Not sure what I'm missing.
Current State of the Activity Stack
SplashActivity->MainActivity
Desired State of the Stack
MainActivity

Have you tried finishing the current activity?
if (haveCredentials()) {
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish(); //add this!
}

Just use finish() after startActivity(), so that when back button is pressed the previous activity will not be shown.

Related

Cancel Transition animation on Activity destroy

I have a splash screen that goes to my main activity and uses a transition animation.
I want to know how to stop the animation if I 'swipe' the application from my app list. Currently if I do this you will still see the animation on top of my phone's home screen.
Splash screen:
intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
finish();
I have tried some of these, but with no success:
overridePendingTransition(0, 0);
or
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
or
finish();
etc.
Any ideas?

OverridePendingTransition doesnt work if i will use intent

I'm trying to open an already activity with animation open.
It will run it only first time which activity starts and then it will not work again
Here is the code :
var intent = new Intent(this, typeof(TableItemsMain))
.SetFlags(ActivityFlags.ReorderToFront);
StartActivity(intent);
OverridePendingTransition(Resource.Layout.trans_left_in, Resource.Layout.trans_left_out);

Restart Android app to specific activity

I want to restart my Android app, but I want it to go to a specific activity after the app restart.
I am currently using this code to re launch my app, but it starts back to the first activity. I need it to go to another specific activity.
I made a settings page in my application where users can choose their own app colors. After choosing a color, i need to restart all activities to apply the new theme that the user chose.
code I am using to restart app to first activity:
Intent startActivity = new Intent();
startActivity.setClass(ProfileSettingsActivity.this,ProfileSettingsActivity.class);
startActivity(startActivity);
finish();
easy
Intent intent = new Intent(this, ANOTHER_SPECIFIC_ACTIVITY.class);
this.startActivity(intent);
finish();
In the onCreate() of your first activity, check for whatever conditions you want, and then based on those conditions launch the intent for your specific activity before the setContentView() is called in the first activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(CONDITION_1) {
startActivity(new Intent(this, ActivityA.class));
} else if(CONDITION_2) {
startActivity(new Intent(this, ActivityB.class));
}
setContentView(R.layout.activity_first);
}
Have you tried clearing all activities and launch the new one with Flags?
Intent intent = new Intent(this, NEW_ACTIVITY.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Doing this, you are clearing all activities previously created and launch NEW_ACTIVITY.class with the new configuration.

How startActivityForResult 2 Activity from 1 parent Activity and get onActivityResult when only 1 child is finisched

I have 3 Activities:
A: Parent Activity
B: first Child Activity
C: second Child Activity
In A Activity open B Activity when button is clicked
Intent i = new Intent(this, ActivityB.class);
startActivityForResult(i, 1);
In A Activity there is a Runnable object that in some condition open C Activity
Intent i = new Intent(this, Activityc.class);
startActivityForResult(i, 2);
All work and on device i see C Activity over B Activity over A Activity but when i close C Activity with this.finish(); in A Activity onActivityResult() event is not fired. It'll be fired only if i close the B Activity too.
How can i know in A Activity when C Activity is closed and let B Activity opened?
Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish. to do so apply the following code in ur project
Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code finishes all the activities except for FirstActivity. Then we need to finish the FirstActivity's Enter the below code in Firstactivity's oncreate
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
Please See this

Finish all called Intents

I got two activities, one active and other paused and I want to close/finish all the two activities and start another intent. I tried this inside the active activity:
finish();
Intent intent = new Intent(getApplicationContext(), Init.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
then I tried to close the program inside Init class and it appeared me the previous paused activity. How can I solve this problem ?
Try calling not calling finish() before you launch the newest intent and use these flags
Intent intent = new Intent(getApplicationContext(), Init.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Categories

Resources