skip to Main Content

I have MainActivity that contain a button to launch SecondActivity. If am on second activity & move the app to background then bring it to foreground immediately & then press the back button it closes the app instead of showing MainActivity.

When I relaunch the app by pressing the app icon it shows me SecondActivity & again when I press back button it closes the app. For the normal flow have to kill the app first then reopen the app. If I don’t kill the app whenever I open it it will launch SecondActivity.

Facing issue on Android 12.

Launching the SecondActivity through intent without setting any flags.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

If I directly press the back button on SecondActivity without taking the app in background then its works normally i.e on press of back button it shows the MainActivity.

Issue occurs only when I put the app in background then bring it to foreground & press the back button.

Please suggest the solution. Thanks.

2

Answers


  1. Have you call finish() below startActivity()?
    Please remove this.
    Or show more code in your MainActivity and SecondActivity for more information!

    Login or Signup to reply.
  2. Try this demo code:

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button launchSecondActivityButton = findViewById(R.id.launch_second_activity_button);
            launchSecondActivityButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            });
        }
    }
    

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/launch_first_activity_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Launch Second Activity"
            android:layout_centerInParent="true"/>
            
    </RelativeLayout>
    

    SecondActivity.java

    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
        }
    }
    

    activity_second.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
       
        <Button
            android:id="@+id/launch_second_activity_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Launch Second Activity"
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>
    

    AndroidManifest.xml

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapp">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    
            <activity android:name=".SecondActivity">
                <!-- Here code -->
            </activity>
    
        </application>
    
    </manifest>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search