当我使用设备的后退按钮从我的第二个Activity
下方返回我的第一个MainActivity.java
(MainActivity2.java
)时,我的第一个{{1}每次调用Activity
方法,但如果我使用onCreate()
后退按钮,则不会调用ActionBar
方法。
MainActivity.java
onCreate()
MainActivity2.java
package exam.mcq.it.intentdemo;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this,"Create method call",Toast.LENGTH_SHORT).show();
Button btn=(Button)findViewById(R.id.btnClick);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in=new Intent(MainActivity.this,MainActivity2.class);
startActivity(in);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AndroidMainfest.xml
package exam.mcq.it.intentdemo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity2 extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我有同样的问题。我使用了一个小黑客,不确定这是否是正确的方法。当按下箭头时,我调用了onBackPressed()方法。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_save:
save();
return true;
case android.R.id.home:
//Pressing on up Arrow was calling onCreate of MainActivity
getActivity().onBackPressed();
return true;
}
你的问题不明确所以我假设你不想在按下actionBar中的箭头时调用onCreate()。
答案 1 :(得分:-1)
覆盖活动中的onBackPressed ()
功能。使用背按按钮时将调用该功能。另外,请参考docs