当前位置: 动力学知识库 > 问答 > 编程问答 >

Android - How can I go back to previous activity from layout

问题描述:

Here I am unable to open previous activity i.e., my_layout1 on back pressed.

This is my code:

public class Main extends Activity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.my_layout1);

Button button= (Button) findViewById(R.id.buttonId);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

setContentView(R.layout.my_Layout2);

}

});

}

@Override

public void onBackPressed() {

finish();

}

}

Please give me suggestions on how to open previous activity layout from current layout.

网友答案:

It appears you have just 1 activity here, not 2. Calling setContentView again in your onClick does not create a new activity or add anything to the backstack. So when you click back, or call finish(), the activity simply ends.

If you want to dynamically inflate various layers within an activity, look into using fragments (you can add fragments to the back stack to get the result you want when pressing back). However the easier option is to navigate to another activity class in your onClick (via an intent) in which you inflate my_Layout2.

So create another activity class (e.g. Main2) similar to this one in which you inflate my_Layout2 (don't forget to register Main2 it in the manifest). Then from your onClick in Main:

Intent intent = new Intent(Main.this, Main2.class);
startActivity(intent);
网友答案:

In onBackPressed instead of finishing the activity change your layout to my_layout1.

Declare a global variable int count=0;

In your onClick add this line count=0;

Change your onBackPressed like this:

 @Override   
 public void onBackPressed() {
  if(count==0){
    ++count;
    setContentView(R.layout.my_layout1);
  }
  else
  finish();
  }
网友答案:

if you want to go back to previous activity set flagLayoutControl as true and then write below code

public boolean onKeyDown(int keyCode, KeyEvent event)
{
    // pass to if back is pressed     
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        if(flagLayoutControl)
        {
              Intent myIntent = new Intent(currentActivity.this, <<WhatYouWant>>.class);
              startActivityForResult(myIntent, 0);
          finish(); 
        }
        else
        {

            Intent myIntent = new Intent(currentActivity.this, currentActivity.class);
                    startActivityForResult(myIntent, 0);
                finish(); 

        }

        return true;
    }
    return super.onKeyDown(keyCode, event);
}
网友答案:

When you press back you are finishing current activity with all layouts on it. In on back press you should create new intent and start this activity. Or

setContentView(R.layout.my_layout1);
网友答案:

create a button and Try this:

@Override

public void onClick(View arg0) {

   onBackPressed();


}
网友答案:
@Override  
public void onBackPressed()   
{  
   super.onBackPressed();
   startActivity(new Intent(my_Layout2.this, my_layout1 .class));
   my_Layout2.this.finish();         
}
网友答案:

Instead of using this

 @Override
    public void onClick(View v) {
        setContentView(R.layout.my_Layout2);
    }

Use this

 @Override
    public void onClick(View v) {
        this.finish();
    }

This close your current layout and show the top most layout

网友答案:

please use below code

public boolean onKeyDown(int keyCode, KeyEvent event)  
{  
     //replaces the default 'Back' button action  
     if(keyCode==KeyEvent.KEYCODE_BACK)  
     {  

       finish(); 
     return true;  
   }  
网友答案:

Put it on back button click.

Intent intent = new Intent(Main Class.this, 
Name of classactivity on which you want to go.class);
startActivity(intent);
分享给朋友:
您可能感兴趣的文章:
随机阅读: