簡體   English   中英

返回導航按鈕沒有功能

[英]Back navigation button no functionality

我瀏覽了許多堆棧溢出問題,但我無法為我的后退導航按鈕提供功能。 可能是什么錯誤

如果有人可以幫助我,因為我明天必須提交我的項目

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

您應該在清單 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>

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

試試下面的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM