2

I have 2 layouts and i can acess page1 and page 2 from layout 1 and layout 2 can be accesed from layout 1 by any page. Now the problem is when i access layout 2 form page2 (i.e Good bye) and then i press the back button i return back to page 1(i.e Hellow World) rather than page 2, how can i return back to page 2 rather than page 1?

here is my Mainactivity function

  public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button next,layout;
    TextView string;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        next = findViewById(R.id.button);
        layout = findViewById(R.id.button2);
        next.setOnClickListener(this);
        layout.setOnClickListener(this);
        string = findViewById(R.id.textView);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button: {  string.setText("Good bye");
            break;}

            case R.id.button2 :{
                Intent stat = new Intent(this, laayout2.class);
                startActivity(stat);
            }
        }

    }
}

and here the layout 2 java

        public class laayout2 extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_laayout2);
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    // Respond to the action bar's Up/Home button
                    case android.R.id.home:
                        Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
                        //main.startProcess(1);
                        break;
                }


         return super.onOptionsItemSelected(item);
        }
    }

and here the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="to Layout2"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

here is example image

page 2 layout2

Akash Jain
  • 684
  • 9
  • 23

1 Answers1

0

The issue is that you are having 2 activitys. What you are calling page1 and page2 is based on what i see in your code only that you are setting a different text onClick in your MainActivity.

Now if you come back from your laayout2 activity onCreate of the MainActivity is called and will set the default text from your xml file. (Here a link to understand the activity lifecycle https://developer.android.com/guide/components/activities/activity-lifecycle)

For your Problem i see 2 possible solutions.

Solution 1: If u have changed your text in MainActivity before going to laayout2 activity u can save the text in savedInstanceState. If u come back and savedInstanceState is not null in onCreate you can set the text to what it was before. The problem of this solution will be then there is no way back to initial state of the text. Here an example how to use saveInstanceState How to use onSavedInstanceState example please

Solution 2:

Instead of changeing text prgrematicly u make 3 fragments for page 1, page2 and one instead of your laayout2 activity. Now u populate the home fragment (in your case page1) in the MainActivitys content view (usally a FrameLayout) and from there you open the other fragments by with fragment transaction. There you will have a backstack that onBackPressed automaticly leads you back to the fragment that was open before. Thats the way i would make it. Here some usefull links about fragments and fragment transactions.

https://developer.android.com/guide/components/fragments

Replacing a fragment with another fragment inside activity group

Good luck, i hope this will help you.

Daniel Spiess
  • 222
  • 1
  • 13