Move between simple actions

Hi

fig

I want to know how this concept is implemented in android

  • Navigation in this application is as follows:

Activity1- - to- β†’ Activity 2-to - -. > Action 3 - - - β†’ Activities2

But at the same time:

I am transferring data from

Activity1 - β†’ Activity 2- β†’ I do not transfer data from Activity2 - β†’ Activity3

Now

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

  • Application closes because Activity2 expects data that is not in Activity3
  • How can I overcome this, how can I save the state (even data) of Activity 2 before moving back from Activity3

The cycle should be:

Activity1- - to- β†’ Activity 2-to - -. > Action 3 - - - β†’ Activities2


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

I hope I understand my description


I gave a sample program to support my question

how to change the 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); } }); } } 
+4
source share
3 answers

Maybe this will help

You have R.layout.activity1 , R.layout.activity2 and R.layout.activity3 , in which you need a separate button for everyone, but you specified R.id.activity3button3 as one button for everyone, in order to better create three buttons for three actions

 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); } }); 
+2
source

There are several options you can use.

Method 1:

In Activity 2, you can save some data using onSaveInstanceIndtance (). This function is called only until onDestroy. In onCreate (), you can retrieve stored data using onRetroreInstanceState ()

Method 2:

You can save data in sharedPrefernces. At any time of any activity, you can get the data stored in sharedPreference.

Method 3:

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

You can use one of them according to your needs.

+2
source

Solution 1 is to save the transmitted 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 store the value and will be available to all applications.

 public class DataHolder { public static String hi; } 

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

 String hi = DataHolder.hi; 

These are two things that first occurred to me.

0
source

Source: https://habr.com/ru/post/1498741/


All Articles