6

Hello

fig

I want to know how this concept is implemented in android

  • Navigation in this application takes place like this::

Activity1- -- to- - -> Activity 2- -to - -.>Activity 3 - - to - -> Activity2

But on doing this ::

I pass the data from

Activity1 - - > Activity 2- -> I do not pass the data from Activity2 - - > Activity3

Now

If I navigate back to Activity2- - from - - Activity3

  • The application closes because Activity2 is expecting the data which is not present in Activity3
  • How can I overcome this, how can I preserve the state(even data) of activity 2 before navigating back from Activity3

Cycle has to be ::

Activity1- -- to- - -> Activity 2- -to - -.>Activity 3 - - to - -> Activity2


  • How to achieve this?
  • What concepts do i need to look for

I hope i am clear with my description


I have given a sample program to support my question

how to modify code to achieve this

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity1);

        Button BTN=(Button) findViewById(R.id.activity3button3);
        BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        Intent INT=new Intent(MainActivity.this,Activity2.class);
                INT.putExtra("hi", "HI");
                startActivity(INT);     
            }
        }); 
    }

 }

Activity2.java

public class Activity2 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);



        Button BTN=(Button) findViewById(R.id.activity3button3);

        BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent INT=new Intent(Activity2.this,Activity3.class);
                startActivity(INT);

            }
        });

    }



}

Activity3.java

public class Activity3 extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity3);

        Button BTN=(Button) findViewById(R.id.activity3button3);

        BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent INT=new Intent(Activity3.this,Activity2.class);
                startActivity(INT);
            }
        });



    }



}
Hariharan
  • 24,741
  • 6
  • 50
  • 54

3 Answers3

3

There are few options you can use.

Method 1:

In Activty 2 you can save some data using onSaveInstanceIndtance(). This function is just called before onDestroy. In onCreate() you can retrieve the saved data using onRetroreInstanceState()

Method 2:

You can save data in sharedPrefernces. At any point in any activity you can get the data saved in sharedPreference.

Method 3:

You can start your activity2 with "singleTask" flag. So while coming back from Activity 3 to Activty 2, previous instance will be called from stack. In this case onCreate() wont be called for Activtiy 2. Rather onNewIntent() will be called.

You can use one of them as per your need.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sushil
  • 8,250
  • 3
  • 39
  • 71
2

May be this will help

You have R.layout.activity1, R.layout.activity2 and R.layout.activity3 in that you need give separate button for all but you have given as R.id.activity3button3 one one button for all so better create three button for three activity

Button BTN=(Button) findViewById(R.id.activity1button1);
        BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                     Intent ide = new Intent(MainActivity.this,Activity2.class);
                     ide .putExtra("hi", "HI");
                 ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     startActivity(ide);
            }
        }); 



  Button BTN=(Button) findViewById(R.id.activity2butto2);
            BTN.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                         Intent ide = new Intent(Acitvity2.this,Acitvity3.class);
                         ide .putExtra("hi", "HI");
                         ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                         startActivity(ide);
                }
            }); 

Button BTN=(Button) findViewById(R.id.activity3button3);
        BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                     Intent ide = new Intent(Acitvity3.this,Acitvity2.class);
                     ide .putExtra("hi", "HI");
                 ide.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                     startActivity(ide);
            }
        }); 
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

Solution 1 is to keep passing value:

Intent INT=new Intent(Activity2.this,Activity3.class);
INT.putExtra("hi", getIntent().getStringExtra("hi","error");
startActivity(INT);

and again

Intent INT=new Intent(Activity3.this,Activity2.class);
INT.putExtra("hi", getIntent().getStringExtra("hi","error");
startActivity(INT);

Solution 2 is to create one static variable that will hold value and that will be accessible all across application.

public class DataHolder {
    public static String hi;
}

and in Activity2 instead of fetching value from Bundle, just get it from DataHolder:

String hi = DataHolder.hi;

These are two things that first came on my mind.

Bozic Nebojsa
  • 3,606
  • 1
  • 19
  • 17
  • Your First Solution is Very good thinking .... thanks for sharing the information :) –  Aug 24 '13 at 17:16
  • 1
    Thank you. But be careful, because it can be messy. You should, also, consider @Sushil 's 3th solution and set launchMode = "singleTask". If it works for you, it is most elegant solution. – Bozic Nebojsa Aug 24 '13 at 17:28