0

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, and when the app is opened again, seems shared performance is cleared.

I use editor.apply(), but still not working.

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.

Sufian
  • 6,405
  • 16
  • 66
  • 120
Erfan
  • 3,059
  • 3
  • 22
  • 49

2 Answers2

0

You just save these values to Sharedpreference in onPause() method :)

Android Shared preferences example

Update:

I usually create Preferences class (Singleton pattern) which exposes getters and setters to quickly read and save values to SharedPreferences. I inject the same instance of Preferences anywhere in the code using Dagger as Dependency Injector.

Why do we should use singleton pattern: Why use a singleton instead of static methods?

I will give you an example of Preferences class:

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);
    }
}

And use it anywhere you want:

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
        }
    }
Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
  • 1
    so what i must do ? if u have suggest tell me exact – Erfan Aug 02 '16 at 09:34
  • @erfan Hi, If you want to save these value even when press home button, just override onPause() method and call storeCurrentActivity() method. This basically about android activity-lifecycle: https://developer.android.com/training/basics/activity-lifecycle/pausing.html – xxx Aug 02 '16 at 09:39
  • 1
    hi dude thanks for help i am new in android and my English is week if u want help please write codes and tell me where must put theme :'( – Erfan Aug 02 '16 at 09:47
  • @erfan I updated and gave you a sample code above. I think you are new to android and you need more time to improve your skills. Lets start with the basics :) – xxx Aug 02 '16 at 09:57
  • ok now i am so confused i think you and your codes cant help me . u said :You just save these values to Sharedpreference in onPause() method :) . i dont have even onPuse() method – Erfan Aug 02 '16 at 10:13
  • Don't worry and try hard :) – xxx Aug 02 '16 at 10:14
  • i do . and if i cant some one find and solve my problem some on pro not explain link paster like you ;) – Erfan Aug 02 '16 at 10:17
0

you are putting 0 as default value in setting sharedPreference and in if condition checking is it false...

PREFS_NAME = "MyPrefsFile"; and

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

are matching different conditions. changed sharedpreference name

  • i think you understand me bad . i think problem is in sharedPreference in p1 not Mainactivity – Erfan Aug 02 '16 at 09:36