Reputation: 123
I have finally finished the app that I was working on. It is a scoreboard app for basketball. It works exactly as expected but during the development phase I have hard-coded all of the variables in. This is fine for me but if this app is useful for anyone else then some of the variables need to be able to be changed.
I figured the best way to do this is with the us of the Options Menu. I've got the menu appearing on the screen when the user clicks on the Settings option in the menu. I created a new Activity called SettingsActivity and was able to show this XML layout by using the startActivity(R.layout.settings) method. But when I click the BACK button on my device it exits the program.
I'm curious of a couple of things... My understanding is that if I use the startActivity() method that it will essentially erase my current Activity and go to the new one. However, in the unlikely situation a user does this in the middle of the game, I don't what it wiping everything out. I need it to just continue where it left off from when the user hits the BACK button.
How should I go about accomplishing this?
Upvotes: 1
Views: 133
Reputation: 3759
You should have your "settings" activity as a PreferenceActivity (http://developer.android.com/reference/android/preference/PreferenceActivity.html) rather than just a normal Activity, assuming it's not too complicated. It really depends what your Preference screen entails.
public class Prefs extends PreferenceActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Within your res/menu folder you can have a menu.xml corresponding to what will be shown when a user presses the Menu Button:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/miPrefs" android:title="Preferences" android:icon="@android:drawable/ic_menu_preferences"></item>
</menu>
Then you can tie this into your Main Activity with this:
@Override
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
Then FINALLY, within your Main Activity, you can add this:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item){
switch(item.getItemId()){
case R.id.miPrefs:
startActivity(new Intent().setClass(this,Prefs.class));
break;
}
}
This whole procedure will allow you to open a PreferenceActivity (it'll open over your Main Activity) then once you go back, you'll be back at your unchanged MainActivity.
If this isn't the approach you're looking for, you'll have to do it using onSaveInstanceState and restore it in onCreate when savedInstanceState != null.
Upvotes: 1
Reputation: 3453
I assume firstly you're using startActivity on an Intent which points to your new class/new activity rather than a XML layout. If you want to save some data use sharedpreferences in the onpause method which you can reload onresume but it should remain unless it is closed by you or Android if memory is running low.
Upvotes: 0