1

I am trying to send one string through shared preferences to another activity. I want to call the same activity back.

I have multiple activities which calls one activity in common. So I want to Identify from which the common activity has been called and want to go back to the same activity from which it is called.

This I have done in 1st Activity:

    SharedPreferences mPrefs = getSharedPreferences("Type", 0);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putString("gosend","1");
    editor.commit();

In 2nd activity

SharedPreferences mPrefs = getSharedPreferences("Type1", 0);
    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putString("goride", "2");
    editor.commit();

In common activity

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


            SharedPreferences mPrefs = getSharedPreferences("Type", 0);
            activityType = mPrefs.getString("gosend", "1");

            SharedPreferences mPrefs1 = getSharedPreferences("Type1",0);
            goride = mPrefs1.getString("goride","2");

            if(activityType.equals("1")) {

                intent = new Intent(ChooseFromMapActivity.this, GoSend.class);

                startActivity(intent);
            }

            if(goride.equals("2"))
            {
                intent = new Intent(ChooseFromMapActivity.this, GoRideActivity.class);

                startActivity(intent);
            }

        }
    });
}

Now when I am calling common activity from 1st activity , I am not returning back to the same rather 2nd activity is getting called.

whats going wrong??

Edit

I tried this : Still dose not work

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


            SharedPreferences mPrefs = getSharedPreferences("Type", 0);
            activityType = mPrefs.getString("gosend", "0");

         //   SharedPreferences mPrefs1 = getSharedPreferences("Type1",0);
          //  goride = mPrefs1.getString("goride","0");

            switch (activityType){

                case "0":
                intent = new Intent(ChooseFromMapActivity.this, GoSend.class);

                startActivity(intent);
                    break;
                case "1":

                intent = new Intent(ChooseFromMapActivity.this, GoRideActivity.class);

                startActivity(intent);
                    break;
            }

            }
  • 3
    Your both `if` statements are called one by one, thats why second activity is being called... use `if else` – Sharp Edge Jan 07 '16 at 06:58
  • 1
    you can use SharedPreferences mPrefs = getSharedPreferences("Type", 0); inb all activities. and in common activity use if else – Pavya Jan 07 '16 at 07:03
  • Thank you for reply.. I used else if.. by this i am getting 1st activity back when i am calling from first but with second activity also I am getting first activity.@ Sharp Edge –  Jan 07 '16 at 07:05
  • Thank you for reply.. Can you please show me in code? @ user3676184 –  Jan 07 '16 at 07:06
  • It really looks like you should be using `startActivityForResult()`, and then you won't need any of this code. Take a look here: http://stackoverflow.com/a/10407371/4409409 – Daniel Nugent Jan 07 '16 at 07:25
  • 1
    you are setting 2 keys in preferences, `goride` and `gosend`, both `if-statements` could be true, also you have `Type` and `Type1` which i am not sure what they are, but there you have like 2 different preferences? hint: use intent extra string to pass SRC_ACTIVITY param, so in common activity you can know which activity called the common one. – Yazan Jan 07 '16 at 07:35

2 Answers2

0

Try the following code in your common activity:

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


            SharedPreferences mPrefs = getSharedPreferences("Type", 0);
            activityType = mPrefs.getString("gosend", "0");

            SharedPreferences mPrefs1 = getSharedPreferences("Type1",0);
            goride = mPrefs1.getString("goride","0");

            if(activityType.equals("1")) {

                intent = new Intent(ChooseFromMapActivity.this, GoSend.class);

                startActivity(intent);
            }

            else if(goride.equals("2"))
            {
                intent = new Intent(ChooseFromMapActivity.this, GoRideActivity.class);

                startActivity(intent);
            }

        }
    });
}

Let me know is your problem is solved. Your default value matches with the required value so every time first if statement will become true.

Hope this helps.

Kiran
  • 96
  • 2
  • 10
0

you are calling the same sharepreference while writing both files

note from 1st activity -->> SharedPreferences.Editor editor = mPrefs.edit();

note from 2nd activity -->> SharedPreferences mPrefs = getSharedPreferences("Type1", 0);

and in your common activity... -->SharedPreferences mPrefs = getSharedPreferences("Type", 0); activityType = mPrefs.getString("gosend", "1");

        SharedPreferences mPrefs1 = getSharedPreferences("Type1",0);
        goride = mPrefs1.getString("goride","2");

you are referring to the same file as in your code, BUT really, what you were trying to do is to refer to 2 seperate sharepreference...

mPref & mPref1....

so, you have to decide to use either one, but not two...

EDIT --->> UPDATE CODE SUGGESTION.

in your Activity 1

SharedPreferences mPrefs = getSharedPreferences("Type", 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("gosend","1");
editor.commit();

in your Activity 2

SharedPreferences mPrefs = getSharedPreferences("Type1", 0);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("gosend", "2");
editor.commit();

==>> make sure the "gosend" values are the one you wanted. you are taking measure on the changes made to "gosend", because in your code, you wanted to test the "gosend" values, and open different activities based on the values.

in your common activity

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

        SharedPreferences mPrefs = getSharedPreferences("Type", 0);
        activityType = mPrefs.getString("gosend", "0");

     //   SharedPreferences mPrefs1 = getSharedPreferences("Type1",0);
      //  goride = mPrefs1.getString("goride","0");

        switch (activityType){

            case "0":
                intent = new Intent(ChooseFromMapActivity.this, GoSend.class);
                startActivity(intent);
                break;
            case "1":
                intent = new Intent(ChooseFromMapActivity.this, GoRideActivity.class);
                startActivity(intent);
                break;
        }

    }
}

** good luck, hope this help..

Chim Ly
  • 159
  • 1
  • 1
  • 11
  • Don't i need to set shared preference in every activity?? Can you please show me in code what's wrong? @Chim Ly –  Jan 07 '16 at 09:14
  • in your case, may I suggest that you use only 1 shared preference file for all your activities, referring to the same shared preference file each time you wanna get the values. and YES, you will need to declare shared preference in each activities, see, shared preferences save in values pair, and u can put many key value pairs in it, and retrieving them at you wish. – Chim Ly Jan 07 '16 at 09:18
  • pls look at your code closely, you are referring to "mPref" and "mPref1", which means there are 2 distinct share preference file, and that's where the problem is. alternatively, you may also want put it as a different java class (option) – Chim Ly Jan 07 '16 at 09:21
  • Plese refer edit. I tried using single mpref still dose not work –  Jan 07 '16 at 09:24
  • Still getting second activity in return when I call from first. –  Jan 07 '16 at 09:41
  • in your case statement, there are condition sets for "0" and "1", but the activity which you are asking has value of "2"... you need to add another case value for "2".... – Chim Ly Jan 07 '16 at 09:46
  • so what will it call on case 0?? On 0 activity 1,, on 1 activity 2 or i tried only case 1 and 2 –  Jan 07 '16 at 09:47
  • perhaps call 1 to open activity 1, call 2 to open activity 2, and if it is not 1 or 2, then return an error message or something. if you are absolutely positive that it only returns either 1 or 2, then u can delete the coding for 0. – Chim Ly Jan 07 '16 at 09:49
  • I tried calling case 1 and 2 it returns 1st activity when i call from second –  Jan 07 '16 at 09:52
  • probably u should post your whole code, and description of what you wanna do in more details... i can't get what u are trying to do... is it still share preference not sharing the values or the program workflow has problem ? – Chim Ly Jan 07 '16 at 09:55
  • I tried to add more activities and checked if it goes back on same activity or not. all others runs well instead of 2nd activity. 2nd activity returns the activity which we called before it. –  Jan 07 '16 at 10:52