0

I have a problem that is annoying me. I have made an application that has two activitis. The first activity has a button and when you press the button it starts the second activity. I'm in the second activity and I press the home button to exit the app, and when I reopen the app the activity launched it's the first one. I want to launch the activity where the app was closed, in this case the second one. How can I do it?

public class FirstActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        View bub = findViewById(R.id.card_ub);



        bub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(FisrtActivity.this, SecondActivity.class);
                startActivity(intent1);

            }
        });




    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button



        }
        return super.onOptionsItemSelected(item);
    }
}

Second Activity -

public class SecondActivityActivity extends ActionBarActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        View bub = findViewById(R.id.card_ub);



        bub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(FSecondActivity.this, FirstActivity.class);
                startActivity(intent1);

            }
        });




    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button



        }
        return super.onOptionsItemSelected(item);
    }
}

Manifest file -

    <application
        android:allowBackup="true"
        android:icon="@drawable/dc"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SecondActivity"
            android:label="Example"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme">
        </activity>

    </application>

</manifest>
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
Alex
  • 35
  • 5

2 Answers2

0

Check developer page for better understanding of backstack.

http://developer.android.com/guide/components/tasks-and-back-stack.html

just call startactivity(new Intent(FirstActivity.this, SecondActivity.class). if you shoould not call finish() when starting new activity. if you are using intent flags like CLEAR_FLAG it will destroy your activity when starting the second activity.

ugur
  • 3,604
  • 3
  • 26
  • 57
0

Check your phone setting if you setup "Don't keep background process". In your case, seems your phone system always kill your application when entering background.

So, if your phone always does that, you can do two things:

  1. close this setting.

  2. Or, you can save activity status in SecondActivity, e.g, onPause / onStop, to let your application know that your current status is SecondActivity.

Next time you open the application, check your application status in FirstActivity, and restore the status to SecondActivity.

Chauyan
  • 157
  • 8