I'm currently moving slowly but steady forward in the making of a Android application and Im currently learning how to create a new window and switch to it. This is going all well but I have one small problem. When I pressing the "go back" button closes the application even if I have choosed to go back when just that button is pressed.
@Override
public void onBackPressed() {
finish();
return;
}
Have I missed something or what?
Thanks in advance.
EDIT
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle item selection
switch (item.getItemId())
{
case R.id.menuItem1:
setContentView(R.layout.about);
return true;
case R.id.menuItem2:
System.exit(0);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EDIT 2: About.java
package weather.right;
import weather.right.now.R;
import android.os.Bundle;
public interface About {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
System.exit() or setContentView() in your MenuItems. You're calling finish() when you override the Back button, so whatever Activity is currently running when you press it will close - adneal 2012-04-04 00:06
System.exit(0) closed the application for real unlike finish(); which I used before I switched to System.exit(0). What should I use - Erik 2012-04-04 00:09
You need to use Intents to "switch windows". A "window" is called an Activity in Android and you set the visual content of an Activity using the setContentView() method in the onCreate() call of the Activity.
To launch another Activity from your current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:
Intent i = new Intent(this, TheNextActivity.class);
startActivity(i);
Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.
Other things to note is that you don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if you want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when you hit the back button.
Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.
This also explains why you're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.
The constructor Intent(new View.OnClickListener(){}, Class<About>) is undefined though when I have changed TheNextActivity to About and added About.java in com.weather.right.now > src > weather.right. I don't know why but do you know why - Erik 2012-04-04 00:23
Intent(NameOfTheCurrentActivity.this, NameOfTheSecondActivity.class) - Michell Bak 2012-04-04 00:24
Abstract methods do not specify a body? on public void onCreate(Bundle savedInstanceState) { in the About.java file. See my edit in the first post for more information. Why am I getting this error - Erik 2012-04-04 00:30