Reputation: 9
I've a simple problem.I'm trying to switch layout between Main Menu and About pages.In Main Menu, there is no problem when i click the "about" button.But in "about" layout, when i click "return to menu" button it just doesn't work.and the code of that layout is in about.java, which also extends Activity.Please have a look.
in MainActivity.java:
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
setContentView(R.layout.about);
}
});
works just fine.But in About.java:
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
nothing happens.I tried every combination of inside onClick() but just doesn't work.What are your ideas?Thanks and have a nice day.
Upvotes: 0
Views: 67
Reputation: 393
In Main Activity,java, it's not starting any activity, it's basically just changing the view. It seems to be working but actually it's not.
You should declare the intent and then call the start activity method. The other activity should have a method onCreate where you can set the content view (using the method setContentView).
It should be something like this:
MainActivity.java
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), About.class);
startActivity(intent);
}
});
About.java
button1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
});
For more information, check this http://developer.android.com/training/basics/firstapp/starting-activity.html
Upvotes: 1
Reputation: 2985
Try to do the same like in your MainActivity in your AboutActivity:
button1_.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
setContentView(R.layout.activity_main); //should be without this line if you set the layout in your onCreate method in the MainActivity (respectively AboutActivity)
startActivity(intent);
}
});
If it works once, should work twice as well
Upvotes: 0