Reputation: 6301
I have 2 activities. The first activity is the LogoActivity
. After 3 seconds I start the second activity that is my MainActivity
.
private void startCountDown(int duration, int interval) {
CountDownTimer mCountDownTimer = new CountDownTimer(duration, interval) {
@Override
public void onTick(long millisUntilFinished) {
// nothing
}
@Override
public void onFinish() {
startActivity(MainActivity.class);
finish();
}
};
mCountDownTimer.start();
}
startActivity(Class mClass)
is a method that I created to start any activity just by giving the class.
Now I am in the MainActivity
. If I exit by pressing home button and return back I see the MainActivity
, but if I press back button from MainActivity
and reopen the app from background the LogoActivity
show up first.
I dont want the users to see the LogoActivity
everytime they press back button(button from phone, not activity) from MainActivity
and restore it from background.
Why is the LogoActivity
shown if I called finish()
?
Upvotes: 0
Views: 3677
Reputation: 122
Always use intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Upvotes: 0
Reputation: 707
You can use activity attributes in manifest to achieve the same.
"android:finishOnTaskLaunch"
For more please visit http://developer.android.com/guide/topics/manifest/activity-element.html
Upvotes: 0
Reputation: 803
Why is the LogoActivity shown if I called finish()?
The answer is- When you start your application than your system checks app's ManifestFile to get An activity whose Intent-filter action and category is set to "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" .
This activity is your app's launcher(first) activity. (I think ,In your case it is LogoActivty).
When you starts your app "Android system" adds this activity in your Activity stack.As you start some other activity that one also add to the top of Activity stack.
On finish an activity it will removed from Activity stack.
whenever you start your app "Android system" traces Activity stack to get top activty.And start your application from that one.
If it found Activity stack empty the system starts your app with your app's launcherActivity.
In your case you finishes both activities.Thus your Activity stack becomes empty and it start you app with LogoActivity.
SOLUTION
override onBackPress method in MainActivity and dont call finish. But In this case your Activity will not closed onbackPress.
Or make your MainActivity launcher activity
Upvotes: 1
Reputation: 4578
Try overiding onBackPressed()
method
@Override
public void onBackPressed() {
moveTaskToBack(true);
}
Upvotes: 0
Reputation: 651
You have to use Intent
for new activity. Like this.
startActivity(new Intent(LogoActivity.this, MainActivity.class));
finish();
Instand of only startActivity(MainActivity.class)
Upvotes: -1
Reputation: 1536
When you press back button, the instance of MainActivity is destroyed.
And then you come back to this task stack again, the LogoActivity is your default Activity so the system creates one instance of it for you.
You can make the MainActivity the default Activity in manifest.xml
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And start LogoActivity in the onCreate method of MainActivity so the user will see LogoActivity first.
After 3 seconds, finish the LogoActivity.
Upvotes: 2
Reputation: 1064
Because it is different using Back vs Home button. See Android Activity lifecycle: http://developer.android.com/intl/es/reference/android/app/Activity.html
Upvotes: 0