重新启动应用时,SharedPreferences无效

时间:2016-08-02 09:18:29

标签: android android-sharedpreferences

我有4项活动:MainActivity,p1,p2,p3。

我的应用程序工作正常,但问题是当app强制停止或在主页按钮中关闭应用程序以关闭时,再次打开应用程序时,似乎共享性能已被清除。

我使用editor.apply(),但仍无效。

MainActivity:

public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page);



        final Button resume = (Button) findViewById(R.id.resume);
        Button next = (Button) findViewById(R.id.next);
        Button exit = (Button) findViewById(R.id.exit);


        resume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                final String PREFS_NAME = "MyPrefsFile";

                SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

                if (settings.getBoolean("my_first_time", true)) {

                    resume.setEnabled(false);

                    Log.d("Comments", "First time");


                    settings.edit().putBoolean("my_first_time", false).commit();
                }else
                {

                    MainActivity.this.finish();

                }
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, p1.class);
                startActivity(intent);
            }
        });

        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });
    }
    }

Xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="resume"
        android:layout_width="wrap_content"
        android:id="@+id/resume"
        android:layout_height="wrap_content" />

    <Button
        android:text="next"
        android:id="@+id/next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="exit"/>
</LinearLayout>

P1:

public class p1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.p1);

        Button next = (Button) findViewById(R.id.next);
        Button home=(Button)findViewById(R.id.home);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(p1.this, p2.class);
                startActivity(intent);

            }
        });

        home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(p1.this, MainActivity.class);
                startActivity(intent);

            }
        });

}
    private void storeCurrentActivity(){
        SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=myPref.edit();
        editor.putString("lastactivity", p1.this.getClass().getSimpleName());
        editor.commit();

    }
    @Override
    public void onResume(){
        super.onResume();
        storeCurrentActivity();
    }

}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:text="next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/next"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="page 1"/>
    <Button
        android:text="go in main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/home"/>

</LinearLayout>

和p2,p3与p1类似。

2 个答案:

答案 0 :(得分:0)

您只需将这些值保存到 onPause()方法中的共享偏好:)

Android Shared preferences example

<强>更新

我通常创建Preferences类(Singleton模式),它公开getter和setter以快速读取并将值保存到SharedPreferences。我使用Dagger作为Dependency Injector在代码中的任何位置注入相同的Preferences实例。

为什么我们应该使用单身模式:Why use a singleton instead of static methods?

我将举一个Preferences类的例子:

public class Preferences {

    private SharedPreferences sharedPreferences;
    private final String NOTIFICATIONS_ENABLED = "NOTIFICATIONS_ENABLED";

    @Inject
    public Preferences(Context context) {
        sharedPreferences = context.getSharedPreferences("Preferences", 0);
    }

    public void setNotificationsEnabled(boolean enabled){
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(NOTIFICATIONS_ENABLED, enabled);
        edit.commit();           
    }

    public boolean isNotificationsEnabled(){
        return sharedPreferences.getBoolean(NOTIFICATIONS_ENABLED, true);
    }
}

并在任何地方使用它:

public class MainActivity extends Activity {

    @Inject
    NavigationController navigationController;

    @Inject
    Preferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedObjectGraph.getObjectGraph().inject(this);

        if(preferences.isNotificationsEnabled()){
            // do something
        }
    }

答案 1 :(得分:0)

您在设置sharedPreference时将0设置为默认值,如果条件检查为false,则为<... p>

PREFS_NAME =“MyPrefsFile”;和

if(settings.getBoolean(“my_first_time”,true))

匹配不同的条件。已更改共享首选项名称