简体   繁体   中英

Android Shared Preferences not working when relaunch app

I have 4 activities: MainActivity, p1, p2, p3.

My app works fine but problem here is that when app force stop or flick up app in home button to close, when the app is opened again, seems shared performance is cleared and my resume button just exit from app .
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>

and p2, p3 like p1.

我只有非共享的经验,但是0可能是错误的标志!

getSharedPreferences(PREFS_NAME, 0); <-- Please use the Constant Context.MODE_WORLD_READABLE

Analysis:

once you have pressed the resume button it is stored as setting["MyPrefsFile", "my_first_time"] = true and resume-button is disabled in current activity instance.

when the activity gets destroyed and is recreated the resumebutton is not initialized from the settings so it is enabled.

to fix add this to you onCreate

 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0)
 resume.setEnabled(settings.getBoolean("my_first_time", true));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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