我在 Android 演示中创建了 4 个活动。当我从第一个活动运行应用程序以显示第二个活动等等时,我试图让数据保持与按下后退按钮时相同的问题应用程序退出的 3 个活动,但是当我返回到第三个活动时我重新启动并且数据保持不变。我使用 Intent 使用共享首选项,但它无法正常工作
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
final SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_1",MODE_WORLD_READABLE);
SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
//shredPref_Editor.putString(MY_ACTIVITY,"splash_activity");
prefName = sharedPref.getString(MY_ACTIVITY,"splash_activity");
handler = new Handler(){};
runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if(prefName == "splash_activity" || prefName.toString().trim().equals("Number_Activity"))
{
Intent i1=new Intent(Splash_Screen_Activity.this,Number_Activity.class);
startActivity(i1);
}
else if(prefName.toString().trim().equals("pin_number_activity"))
{
Intent i2=new Intent(Splash_Screen_Activity.this,PIN_Number_Activity.class);
startActivity(i2);
}
else
{
Intent i3=new Intent(Splash_Screen_Activity.this , Custom_Title_Bar_Activity.class);
startActivity(i3);
}
}
};
public class Number_Activity extends Activity
{
private final String MY_ACTIVITY = "Number_Activity";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cell_number);
SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_2",MODE_WORLD_READABLE);
SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
shredPref_Editor.putString(MY_ACTIVITY,"number_activity");
shredPref_Editor.commit();
}
}
public class PIN_Number_Activity extends Activity
{
private final String MY_ACTIVITY = "PinNum_MyActivity";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pin_number);
SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_3",MODE_WORLD_READABLE);
SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
shredPref_Editor.putString(MY_ACTIVITY,"pin_number_activity");
shredPref_Editor.commit();
}
}
public class Custom_Title_Bar_Activity extends Activity
{
private final String MY_ACTIVITY = "Main_Activity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Boolean customTitleSupported=requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main_for_ctitlebar);
SharedPreferences sharedPref=this.getSharedPreferences("MY_PREF_4",MODE_WORLD_READABLE);
SharedPreferences.Editor shredPref_Editor=sharedPref.edit();
shredPref_Editor.putString(MY_ACTIVITY,"main_activity");
shredPref_Editor.commit();
}
}