2016-08-03 43 views
0

我有4項活動:MainActivity,p1,p2,p3。重新啓動應用時Android共享偏好設置不起作用

我的應用程序工作正常,但這裏的問題是,當應用程序強制停止或輕彈應用程序在主頁按鈕關閉,當應用程序再次打開時,似乎共享性能被清除,我的簡歷按鈕剛剛退出應用程序。
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

第一條:您正在使用兩種不同的'SharedPreferences':'MyPrefsFile'和'APP_SHARED_PREFS'。除此之外:你正在設置'lastactivity'的值,但你永遠不會找回它。 – Bobby

+0

如果你想幫助我,只是告訴我確切的位置,我必須把代碼或編輯。即時通訊新的android和我嘗試​​每一天解決這個爲1周。請幫助我,如果你能解決我的問題 – erfan

回答

0

我有非共享經驗,但0可能是錯誤的標誌!

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

我使用SharedPreferences設置= getSharedPreferences(PREFS_NAME,MODE_WORLD_READABLE);但仍然有這個問題 – erfan

0

分析:

一旦你按下恢復按鈕它被存儲爲setting["MyPrefsFile", "my_first_time"] = true和恢復按鈕在當前活動實例禁用。

當活動被破壞並重新創建時,resumebutton沒有從設置中初始化,因此它被啓用。

修復內容添加到您的onCreate

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

當我添加這2行:不能解決符號PREFS_NAME錯誤! – erfan