【问题标题】:Back navigation button no functionality返回导航按钮没有功能
【发布时间】:2016-04-30 18:47:28
【问题描述】:

我浏览了许多堆栈溢出问题,但我无法为我的后退导航按钮提供功能。可能是什么错误

如果有人可以帮助我,因为我必须在明天提交我的项目

我的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;
}

【问题讨论】:

标签: android eclipse android-studio


【解决方案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>

【讨论】:

  • 如果您不使用自定义操作栏,请将您的项目 id 更改为 android.R.id.home,如 @Arpit 的回答。
  • @ugurB 谢谢,它现在正在工作似乎只声明完成()不起作用,感谢 Arpit
【解决方案2】:

设置后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);
    }
}

【讨论】:

    【解决方案3】:

    试试下面的代码

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
        default:
            return super.onOptionsItemSelected(item);
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多