希望没有人问过这个问题。
在构建应用程序并成功打开主活动的新活动时,按下后退按钮后,应用程序退出而不是返回主活动。
以下是从主要活动中调用的活动的代码。
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);
}
}
这是主要活动
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);
}
}
}
有关方xml摘录
<activity android:name="com.austinrhodes.simplesounds.About" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.austinrhodes.simplesounds.Launch" />
</activity>
答案 0 :(得分:1)
试试这个
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
CurrentClass.this.finish();
Intent i = new Intent(CurrentClass.this, MainActivity.class);
startActivity(i);
return true;
default:
return false;
}