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