0

I have two Activities.Now i want to know how to save the button Click-event , like when i pressed the button want to save boolean value true in Shared Preferenaes , when not pressed the button the boolean value is false in Shared Preferenaes every time whenever go to that second page.Can someone help me how to do this trick.Thanks in advanced.This is my code of Button Click .

  imgBtn_LogOut.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) 
{
 SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("strUserName", true);
                    editor.commit();

                    File database = getApplicationContext().getDatabasePath(dbhelper.DATABASE_NAME);

                    if (!database.exists())
                    {
                        Log.e("Database", "Not Found");
                    }
                    else
                    {
                        Log.e("Database", "Found");
                        getApplicationContext().deleteDatabase(dbhelper.DATABASE_NAME);
                        Log.e("Database", " Deleted Completeley !!!");

                        removeImagesFromFolder();

                        Intent i = new Intent(Filter_Screen.this, Login_Screen.class);
                        startActivity(i);
                        finish();
                    }
                }
            });
Abhishek
  • 1,654
  • 2
  • 18
  • 31
p. ld
  • 585
  • 3
  • 10
  • 23
  • 1
    where is your code?? – M D Dec 18 '15 at 07:19
  • Possible duplicate of [Android Shared Preferences](http://stackoverflow.com/questions/5734721/android-shared-preferences) – Dhinakaran Thennarasu Dec 18 '15 at 07:20
  • @ Dhina : I think you are nor read my question. Please read first and then write the comment.I know what is Shared Preferences and how to use. I want to know how to get button click event in boolean values in true or false whenever go to that page.This is my issue. – p. ld Dec 18 '15 at 07:26
  • @p. ld : Please clarify what you exactly want to do ? When you click on button this already set preference "strUserName" as true. Then what is the problem please clarify so that can help you in better way. – Hitesh Bhalala Dec 18 '15 at 07:32
  • @Dhina: its not a duplicate. – Mr Robot Dec 18 '15 at 07:32
  • See , whenever i go to that particular Activity , i want to save value in true if i pressed button and if not then save false. So how i want to know i click the button or not . – p. ld Dec 18 '15 at 07:39
  • use shared preference in common class as static field, so easy you can edit as well as get data from it. – prakash Dec 18 '15 at 07:47

2 Answers2

0

As simple you can do like this,

Filter_Screen:

imgBtn_LogOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) 
{
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("strUserName", true);
editor.commit();
Intent i = new Intent(Filter_Screen.this, Login_Screen.class);
startActivity(i);
finish();
}
});

Login_Screen:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
sharedPreferences.getBoolean("strUserName",false);
}
Ronak Joshi
  • 1,553
  • 2
  • 20
  • 43
0

you can create,,

Toast.makeText(this,"button select!",Toast.LENGTH_LONG).show();

for all the buttons, it could notify you which button you have selected. OR try using a print in, if and else statements,, may be it work.

Vaibhav Vishal
  • 6,576
  • 7
  • 27
  • 48