简体   繁体   English

如何使用共享的首选项和切换方法进行自动保存?

[英]How to make autosave using shared preferences and switch method?

Lets say, we have variable var, that store information abaut name of activity. 可以说,我们有变量var,用于存储活动的信息名称。 It gets it from shared preferences. 它是从共享首选项中获取的。 I want to make some kind o autosave. 我想做某种自动保存。 Last activity that was initiated send its name do shared preferences. 最后启动的发送其名称的活动将共享首选项。 The thing that i dont now, is how to put the name of activity from variable var to intent in switch method in buttonlastactivity. 我现在不要做的事情是如何将活动名称从变量var放入buttonlastactivity中的switch方法中的intent中。 The first buttonnew goes to first activity that is actbegin. 第一个按钮new转到actbegin的第一个活动。

Example: 例:

public void onClick(View view) {
Intent intent;

switch (view.getId()) {
case R.id.buttonnew:
intent = new Intent(actmenu.this, actbegin.class);
startActivity(intent);
break;
case R.id.buttonlastactivity:
intent = new Intent(actmenu.this, ??????.class);
startActivity(intent);
break;

The question is, what to put instead of question marks, to take name of activity that is stored in variable var. 问题是,用什么代替问号,取存储在变量var中的活动名称。

Any suggestions? 有什么建议么?

You could save the fully qualified class name in your shared preferences and use that . 您可以将完全限定的类名保存在共享的首选项中并使用。
eg. 例如。 : If you save "com.test.MyLastActivity" in your SharedPreferences, then you could do this : :如果将“ com.test.MyLastActivity”保存在SharedPreferences中,则可以这样做:

try {
        Class<?> lastActivity = Class.forName("com.test.MyLastActivity");
        Intent intent = new Intent(this, lastActivity);
        startActivity(intent);
    } catch (ClassNotFoundException e) {
        Log.e("TAG", "Couldn't find class", e);
    }

I did it like this: 我这样做是这样的:

public void onClick(View view) {
      Intent intent;
      switch (view.getId()) {
 case R.id.buttonnew:
      intent = new Intent(actmenu.this,actbegin.class);
      startActivity(intent);
      break;
case R.id.buttonlastactivity:
       try
       {
       Class<?> lastActivity = Class.forName(var);
       intent = new Intent(actmenu.this, lastActivity);
       startActivity(intent);
       break;
       }
       catch (ClassNotFoundException e)
       {
       Log.e("TAG", "Couldn't find class", e);
}

}
}

and it doesnt give errors, but after app start and after pressing the buttonlastactivity (when variable var has full name of last activity) the app is going back to activity that starts app, not to the activity that name is written in variable var. 并且它不会给出错误,但是在应用启动后并按下buttonlastactivity之后(当变量var具有上次活动的全名时),该应用将返回到启动应用的活动,而不是名称写在变量var中的活动。 I want to use vvariable, because it is changing depending on what activity the user was, before he quited the app. 我想使用vvariable,因为它会根据用户退出应用程序之前的活动而变化。 And the variable gets it from shared preferences. 变量从共享首选项中获取。 Shared preferences get the name of activity in moment of starting of the activity. 共享首选项会在活动开始时获得活动名称。 Or is there any other way to do some kind of autosave, that will allow user to go to the last visited activity. 还是有其他方法可以进行某种自动保存,使用户可以转到上次访问的活动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM