Hopefully this hasn't been asked.
In building an app and having successfully opened a new activity from the main activity, when the back button is pressed the app exits instead of returning to the main activity.
Here is the code for the activity that gets called from the main one.
package com.austinrhodes.simplesounds;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.KeyEvent;
import android.view.MenuItem;
public class About extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is the main activity
package com.austinrhodes.simplesounds;
import android.os.Bundle;
import android.app.Activity;
import android.app.DialogFragment;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Launch extends Activity {
int size = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate view from XML
setContentView(R.layout.activity_launch);
createTvArray();
}
private void createTvArray() {
//create array of text views to be added to main view
TextView[] tv = new TextView[size];
//create a temporary text view
TextView temp;
for (int i = 0; i < size; i++)
{
//make a new text view object for each of the alarms
temp = new TextView(this);
temp.setText("Alarm: " + i); //arbitrary task
//get instance of Linear Layout
LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.linearAlarm);
// add the textview to the linearlayout
myLinearLayout.addView(temp);
tv[i] = temp;
}
}
private void openSettings(){
Intent intent = new Intent(this, Settings.class);
startActivity(intent);
finish();
}
private void openAbout(){
Intent intent = new Intent(this, About.class);
startActivity(intent);
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_launch, menu);
return true;
}
public boolean onCreateOptionsMenu1(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_launch, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.settings:
openSettings();
return true;
case R.id.new_alarm:
DialogFragment newFragment = new AddAlarm();
newFragment.show(getFragmentManager(), "add_alarm");
return true;
case R.id.about:
openAbout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
The concerned parties xml snippet
<activity android:name="com.austinrhodes.simplesounds.About" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.austinrhodes.simplesounds.Launch" />
</activity>