我浏览了很多堆栈溢出q / a但我无法给我的后退导航按钮功能。什么可能是错误
如果有人可以帮助我,因为我必须提交我的项目tommorow
我的java代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crime);
TextView disp1 =(TextView) findViewById(R.id.textView2);
disp1.setText("Displaying 1 of 24");
TextView displa = (TextView) findViewById(R.id.textView);
displa.setText(" China is the source of 70% of the worlds pirated goods.,");
getActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_crime, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.what:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,"Hey,I saw an amazing fact from Fact-O-Mania" +"\n"+"\n"+fact[i]);
sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
int id = item.getItemId();
break;
case R.id.home :
finish();
}
return true;
}
答案 0 :(得分:1)
您应该在清单xml中定义父活动,以告知在按下工具栏的后退按钮时导航的位置。
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
答案 1 :(得分:0)
设置actionBar.setHomeButtonEnabled(true);
添加以下代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; goto parent activity.
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
答案 2 :(得分:0)
尝试以下代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}