paul590
paul590

Reputation: 1435

On back button shows same activity over and over

I have activity A which can open Activity B. The problem that I am having is, if I open Activity B and then close Activity B and re open Activity B, when i pressed the back button I will see Activity B again. It seems every time I open the activity the activity gets added on the stack and not being destroyed when using finish() or onBackPressed(). Any help would be appreciated!

This is how I open my activity B from Activity A:

    Intent intent = new Intent();
    intent.setClass(this, ActivityB.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);

Activity B back button is called using:

    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            onBackPressed();
            //finish();
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

Manifest XML:

        <activity
        android:name="com.example.project.FirstActivity"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

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

        </activity>

      <activity
        android:name="com.example.project.ActivityA"
        android:screenOrientation="portrait">
    </activity>

    <activity
        android:name="com.example.project.ActivityB"
        android:screenOrientation="portrait">
    </activity>

Upvotes: 0

Views: 79

Answers (1)

s.mhmd
s.mhmd

Reputation: 307

in mainfest.xml >> add

<activity
        android:name=".ActivityB"
        <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".ActivityA" />

`

Upvotes: 1

Related Questions