简体   繁体   中英

How to go back from second screen to first screen

How to switch layouts? First, I have a class Main where is onCreate ( setContentView(R.layout.main); ) and then I call, another class with command:

setContentView(secondClass);

In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" ( R.layout.main ), but I don't know how to do it.

Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.

In secondClass I can't create onCreate method, but I also tried the following and they didn't work:

Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");

startActivity(abc);

and

Intent abc = new Intent(SecondClass.this,FirstClass.class);

startActivity(greNaPrvoOkno);

If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;

Consider you are showing the second class from your Main activity like this;

setContentView(new SecondClass(getApplicationContext(), MainActivity.this));

And you Second class is this (suppose);

// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {

    // This is needed to switch back to the parent activity
    private Activity mParentActivity = null;

    public SecondClass(Context context, Activity parentActivity) {
        super(context);

        mParentActivity = parentActivity;   
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // Set the Main view back here.
        mParentActivity.setContentView(R.layout.main);
    }

}

Disclaimer: This code will do what you have asked for, but may cause other problems.

As advised by @Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.

On the Onclick event of the button you have to write finish(); that's it..

You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).

For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.

You still need to configure the widgets(eg TextView) on it when you set it back.


You don't have to startActivity again to go back.

Just call finish() in your second activity when you want to finish the current activity and go back:

eg When user press the back button in your second activity

mButtonBack.setOnClickListener(new Button.OnClickListener() 
{       
   public void onClick(View v) 
   {                            
      finish();
   }
}

Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.

When I have used multiple intents in my android application, I have created a new activity through:

Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);

When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:

Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);

However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM