0

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()?

Chris
  • 6,105
  • 6
  • 38
  • 55

8 Answers8

2

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.

Eric Liu
  • 1,516
  • 13
  • 19
  • I tested activity lifecycle and what you said is correct. Would be a good practice to use this method or is there any other alternative? – Chris Nov 19 '15 at 11:09
  • Another to to do it is to use one Activity, 2 Fragments. – Eric Liu Nov 19 '15 at 11:19
  • 2 fragments doesnt worth the effort, so I will use the first method. Thanks for your help. – Chris Nov 19 '15 at 11:23
1

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

Navin Gupta
  • 793
  • 6
  • 20
  • Your answer is very good but I already accepted an answer. I choosed to make MainActivity my launcher activity because I will show LogoActivity just at first app start. Thanks for your activity stack explanations! – Chris Nov 19 '15 at 11:37
0

Because it is different using Back vs Home button. See Android Activity lifecycle: http://developer.android.com/intl/es/reference/android/app/Activity.html

Alberto Méndez
  • 1,044
  • 14
  • 31
0

Try overiding onBackPressed() method

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
0

Try to control the onBackPressed() method of your activity.

  • 2
    Welcome to StackOverflow! Please, expand your answer. Explain your solution in more details, provide an example. Brevity is acceptable, but fuller explanations are better. – naXa stands with Ukraine Nov 19 '15 at 12:06
0

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

Ajitha
  • 717
  • 1
  • 7
  • 30
0

Always use intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

IDontEvenKnow
  • 122
  • 1
  • 1
  • 9
-1

You have to use Intent for new activity. Like this.

startActivity(new Intent(LogoActivity.this, MainActivity.class));
finish();

Instand of only startActivity(MainActivity.class)

Jaydip
  • 592
  • 5
  • 27
  • I said that ``startActivity(Class mClass)`` is a method I created and contains the Intent. So your answer is irrelevant. – Chris Nov 19 '15 at 10:58