简体   繁体   中英

How to go back to a activity through the back action bar button without recreating the Parent activity

I am creating an app where a user can generate a random number from 1 to 90 to play bingo or tambola. Here I set the homeAsUpEnabled as true, and in the Manifest, I have added the support parent activity: this is the main activity:



        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setTitle("Full House: Housie Number Generator");
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);


        ivBoard.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(NumberActivity.this, com.app.fullhouse.Board.class);
                intent.putExtra("doneNumbers", doneNumbers);
                startActivity(intent);
            }
        });

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);

        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.reset:
                new AlertDialog.Builder(this)
                        .setTitle("Reset")
                        .setMessage("Press OK to reset Board")
                        .setNegativeButton(android.R.string.no, null)
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
                        {

                            public void onClick(DialogInterface arg0, int arg1) {
                                recreate();
                            }
                        }).create().show();
                break;
            case R.id.showNoOrder:
                Intent intent = new Intent(this, com.app.fullhouse.NumberOrder.class);
                intent.putExtra("doneNumbers2", doneNumbers2);
                startActivity(intent);

                break;

        }
        return super.onOptionsItemSelected(item);
    }


}

and this is my other activity:

ActionBar actionBar = getSupportActionBar();
        tvnumbers = findViewById(R.id.tvNumbers123);
        assert actionBar != null;
        actionBar.setTitle("Sequence of Number generated");
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        actionBar.setDisplayHomeAsUpEnabled(true);

and part of another:

ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Board");
        actionBar.setDisplayUseLogoEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);

please take into notice that I have cut short much of the unrequired items because the code would get too long

What have I tried: I tried to startActivityForResult and their end activity by guessing that the back button could be android.R.id.home

when I try to press the back button on the phone, noting goes wrong and the previous data is still there but when I press the back button in any of the two-child activities, it goes back, but it recreates the whole activity, hence the game is lost

here is the manifest in case you need it:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.fullhouse">

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/fullhouse"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/fullhouse"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar">
        <activity android:name=".GenerateTicket"></activity>
        <activity android:name=".NumberOrder">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".NumberActivity" />
        </activity>
        <activity android:name=".NumberActivity"></activity>
        <activity android:name=".Board">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".NumberActivity" />
        </activity>
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/Theme.AppCompat.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

as well as the menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/reset"
        android:menuCategory="container"
        android:orderInCategory="104"
        android:title="@string/reset_board"
        app:showAsAction="never" />
    <item
        android:id="@+id/showNoOrder"
        android:title="Show sequence of numbers"
        app:showAsAction="never"

        android:orderInCategory="105"/>

</menu>

thank you in advance

Possibly you can find your answer here
How to come back to First activity without its onCreate() called

And can try below from here

Two cases:

  1. If you want to keep other activities live and just want to bring A to front, just use intent flag FLAG_ACTIVITY_REORDER_TO_FRONT.

  2. If you don't want to clear all activities and want an existing instance of A at the top, then use intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.

Note: If you use only FLAG_ACTIVITY_CLEAR_TOP, then onCreate will be called.

I have found an answer (to all the people who visit this later on:)) the answer is this

Intent intent = new Intent(this, Activity2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT|Intent.FLAG_ACTIVITY_REORDER_TO_FRONT|Intent.FLAG_FROM_BACKGROUND);

startActivity(intent);


and in the manifest file, you don't need to add anything cause here it's working for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM