Reputation: 607
Right now, I have an activity with method PrepareData(), used to prepare every data that needed by current activity, this called in OnCreate before I set everything. I call this method, and when find some issue I want to finish current activity.
So this is snippet of my code:
private void PrepareData()
{
try
{
//some code to prepare data here
}
catch(Exception ex)
{
Intent _startNewActivity = new Intent(this, ActivityB);
this.StartActivity(_startNewActivity);
this.Finish();
}
}
and OnCreate like this
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.ActivityA);
PrepareData()
toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
SetSupportActionBar(toolbar);
SupportActionBar.Title = "Activity A";
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
}
}
Right Now, when app find error on PrepareData, intent is called and this.Finish() also called, but somehow app not finish Activity A immediately, it still set toolbar, and also call onResume.
I know there is activity lifecycle that onStop always called after onResume,But I want to know there is way to finish current activity immediately, without call next code?
Upvotes: 0
Views: 832
Reputation: 3167
An activity always calls through the first lifecycle, even if you call finish, e.g. onCreate --> onStart --> onResume
. Finish call is only scheduled to be performed after onResume
. If your only issue is to prevent some code from executing in onResume, define a flag where you call this.Finish()
, for instance bool finishCalled = true;
and then to prevent the toolbar from setting the title, just wrap the code inside that bool with if !(finishCalled)
.
That should do it.
Upvotes: 1