How to save data and re-open the last used Activity

前端 未结 2 2037
感动是毒
感动是毒 2021-01-16 15:03

I\'ve made the majority of my game\'s mechanics, I now need to be able to:

  1. Save all the data of the current activity and <

2条回答
  •  天命终不由人
    2021-01-16 15:50

    The onSaveInstanceState will not be called when you press back key to finish it. To save your data all the time you have to use onPause as the code shows:

    @Override
    public void onPause() {
        super.onPause();
        SharedPreferences sp = getSharedPreferences("X", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("lastActivity", HomeActivity.class.getCanonicalName());
    }
    

    Remember to replace HomeActivity with your own class name.BTW, I didn't test the function getCanonicalName, but it should work.

    In your Dispatch Activity, you read the value back and do some action to it.

提交回复
热议问题