Calling finish() After Starting a New Activity - android

The first Activity that loads in my application is an initialization activity, and once complete it loads a new Activity. I want to ensure if the user presses 'Back' they go straight to the Launcher, and not the initialization screen. Side note, is this even the best approach, or would this be better done with some kind of Intent Flag?
Is it correct to call finish() after calling startActivity() on the new activity?
onCreate() {
...
startActivity(new Intent(this, NextActivity.class));
finish();
...
}
I'm still taking in the whole 'Message Queue' method of doing things in Android, and my assumption is that calling startActivity() and then finish() from my first Activity's onCreate() will log each respective message in the message queue, but finish execution of onCreate() before moving on to starting the next Activity and finishing my first one. Is this a correct understanding?

Probably you should just use the noHistory flag on the activity in your manifest.xml

Related

Previous Activity still present

Is there a way to totally remove the previous activity?
Explanation:
I have a SplashScreen Activity where I am loading data, etc. and then when it finishes I am navigating to MainActivity like this:
startActivity(Intent(applicationContext, MainActivity::class.java))
finish()
My MainActivity come in foreground, no problem here, but when I am listening to Lifecycle Event with ProcessLifeCycleOwner library, in this particular case:
#OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun appWillEnterBackground() {
Log.e("INACTIVE", "BECOME INACTIVE")
}
Both of my Activities are going on this method while I am on my MainActivity. My SplashScreen Activity should not be able to enter this method because I finish() it.
I tried to return after finish() , I tried "noHistory=true" in Manifest.xml but neither of those worked.
Any suggestions?
You can try finishAffinity() method.
finishAffinity(): Finish this activity as well as all activities immediately below it in
the current task that have the same affinity.
First call finish method and then start new activity.
finish()
startActivity(Intent(applicationContext, MainActivity::class.java));
You can use singletop launch type for MainActivity.
<activity android:name=".SingleTaskActivity"
android:launchMode="singleTask">
Create a broadcast receiver in the activity you want to kill. register it with an intent action like "ACTION_KILL_THIS_ACTIVITY". in onReceive method of this broadcast receiver add finish(); line. When your new activity starts, you are ready to kill the other one so from the new activity's onCreate() method, call
sendBroadcast(new Intent("ACTION_KILL_THIS_ACTIVITY"));
Instead of passing the applicationContext pass the SplashScreenActivity.this as the argument, so your code will look like this:
startActivity(Intent(SplashScreenActivity.this, MainActivity.class));
finish();

Android startActivity(intent) doesn't finish current activity, but runs all the code from current activity

I have a section of code where I want to go to another activity. After parsing these lines of code, the app doesn't close current activity but keeps executing all the onCreate() method from the current activity.
Intent intent = new Intent(ExtraInformationActivity.this,
MainActivity.class);
startActivity(intent);
finish();
Should the finish() function immediately finish current activity and go to the new activity, or the app should execute all the code from the onCreate method? How can I immediately close current activity? Thanks
Calling finish() in an activity does not INSTANTLY close it, it will close it after it runs all of the lifecycle methods and whatever method you called finish() from.
So if you call finish() in onCreate, it will still call onStart, onResume, onPause, onStop and onDestroy (and all of the regular lifecycle methods).
That is how it works, and you can't change that. So you'll need to adjust your code to it.
to avoid running codes after calling finish, add return; after finish();
Read this
void finish () Call this when your activity is done and should be
closed.
When you are using startActivity(), there is no need to use finish().New activity automatically pops-up.

why onCreate() is called every time when its started activity is finished?

In my app, there is a activity A, which it's a main activity, also there are several fragments inside A. When you click the images in one of fragments, it will start a new Activity B. When you click back button, i will call finish() to finish the activity and return to Activity A. But when returning to the Activity A, onCreate() of A is called again. Why onCreate() is called each time? As i know, it should be just called once, and then onStart() should be called.
From segment to the Activity B is as below:
Intent i = new Intent(_scontext, ProductListing.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_scontext.startActivity(i);
getActivity().overridePendingTransition(R.anim.push_out_to_left,R.anim.push_out_to_right);
When click back button in the Activity B, the code snippet is as following:
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
finish();
overridePendingTransition(R.anim.push_out_to_left,
R.anim.push_out_to_right);
What's wrong with the code? Am i missing anything?
You are starting the activity again. Remove the following code and it will work.
Intent _gobck = new Intent(_ctx,ProductDisplay.class);
startActivity(_gobck);
Since you already got your answer by #Rajitha Siriwardena but i just want to clear some of the points here,
As i know, it should be just called once, and then onStart() should
be called.
Above sentence is not a true first of all .
There is possibility to for your ActivityA to go in OnCreate even if you finish your ActivityB. If your ActivityB stay in foreground for a long time ,of-course your ActivityA will be in background in that case , so ultimately your Activity in onStop (remember not in onPause) and android Activity life cycle doc says, after onStop if your app want reach your Activity then it will goes in onCreate
So finish() ActivityB would work but there is no guarantee to your ActivityA called onCreate when you do so .
if you remove finish() from your backPress Activity will not be created and you don't need to write Intent it will manage it's back stack it self.

Android: How to get back to the Calling Activity without using finish()

I call an activity called Activity1 from an Activity called MainActivity using the following:
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
So, when the control comes to Activity1. The normal activity lifecycle is started. i.e onCreate() is called and so on.
when i click the back button when the control is in Activity1. The finish method is called, which in turn calls the onDestroy() and now the control is back with the MainActivity screen.
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed()");
finish();
}
The next time i call Activity1. The onCreate is called again as i called the onDestroy (when i pressed the back button) from the previous call.
Question:
1. is there a way to pass control back to the MainActivity when the back button is pressed without having to call the "finish()" method?
2. problem with calling finish, every time i call Activity1 from MainActivity, A new instance of Activity1 is created. that is the lifecycle again starts from onCreate()..
i do not want this to happen as this is has become a major performance issue.
The main point i'm looking for is whether i can start the activity1 from the resume state rather than oncreate, when i call it after the first time.
I don't believe you need to call the "finish()" method on onBackPressed. Android does that for you when you press the back button. The onBackPressed is used to last minuet tidy up (save stuff to sharepreferences, etc).
Android default behaviour is to call onCreate whenever a new activity is place on the screen. You cannot call a new Intent without this to happen.
I'm not sure why this is performance issue for you. Can you go in a little more detail what activity1 is doing? Are you doing heavy network communication? Is it possible you can cache the store results?
in Actitity1 you define your WebView as:
private static WebView webView = null;
in onCreate() you only create it if it's null:
if(webView == null){
//create webview and load from network
}
Use this approach wisely as it may easly lead to memory leaks if you point to objects in other activities, or objects that may be kept alive (runnables, messages, etc.)

Issue with activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP

I want to finish my app calling the first activity with Intent.FLAG_ACTIVITY_CLEAR_TOP and finishing it. However, when it finishes, the app restarts automatically, and goes directly to Activity 2.
Why? Isn't the stack of activity supposed to be empty after finishing an activity called with Intent.FLAG_ACTIVITY_CLEAR_TOP?
My stack is Activity2>(more activities)>Activity1.
In Activity2
Intent exit_intent=new Intent(context, Activity1.class);
exit_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
exit_intent.putExtra("EXIT", true);
context.startActivity(exit_intent);
In Activity1
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
From the javadoc:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
CLEAR_TOP will wipe out all Activities ABOVE Activity1: if Activity2 is below Activity1 then once Activity1 finishes you will see Activity2.
Are you sure finish() is being called properly in Activity1? I'm getting the feeling it's not, because:
I'm not sure where that if statement goes inside your Activity. It should be in onNewIntent.
If you are making that if statement inside the onNewIntent method, it's still wrong. The docs specify that getIntent() will always return the original intent that started the Activity, unless you call setIntent().
To conclude, maybe something else is getting called in your Activity1 (can't tell without the full code) that starts Activity2 instead of finishing.
If what I described is not the case, and your activity stack indeed looks like Activity2 > Activity1 like the others have described yes, it will not work. Just call finish() in Activity2?
According to the docs:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
So it doesn't clear the entire activity stack, only any other activities that were on top of an old instance of the activity being launched.
You have it right but are you catching the intent inside onNewIntent method of your first activity? also activity 2 should be launched after activity 1 in order for this to work.
From Intent doc:
public static final int FLAG_ACTIVITY_CLEAR_TOP
If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created.

Categories

Resources