jump from main activity to last visited activity

不问归期 提交于 2021-02-19 07:59:39

问题


Lets suppose I am at 4th activity of my app and I have ten activities. I close the activity from 4th level now when i reopen my activity again i have a button on main activity when i click on it I must have to go on 4th activity where i left last time. Now what I do is that I saved every activity number or ID in Shared preference like that

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putInt("level", 4);
editor.apply();

Then i retrieve it in main activity like this:

btnconti.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
      int restoredLevel = prefs.getInt("level", 0);
      if (restoredLevel >0) {
      }
   }
});

Now can any body tell me how i can jump to my last visited activity?


回答1:


If you have seperate Activities for all 10 activities u can call an intent as follows inside multiple if conditions of restoredlevel

Intent i = new Intent(getApplicationContext(),ActivtyOfyourlevel.class)
startActivity(i);


来源:https://stackoverflow.com/questions/30162939/jump-from-main-activity-to-last-visited-activity

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!