正确的方式来处理操作栏上按钮?按钮、栏上、正确、操作

2023-09-12 01:43:20 作者:剩下空心要不要#

我用ActionBarSherlock(虽然我不认为它很重要)。

我有一个主要活动和关于活动。我希望关于活动显示背箭头由其标志,并做适当的动画和这样的。 我不知道如何正确地做到这一点。

目前,我有onOptionsMenuItemSelected下开展的主要活动时的/ home键是pssed $ P $,但它的哈克并没有真正工作的权利。它扮演了错误的动画,并处理多任务不佳。

我如何设置这对吗?

下面是启动关于部分我主要活动:

 意图aboutIntent =新的意图(MainActivity.this,About.class);
MainActivity.this.startActivity(aboutIntent);
 

下面是我关于活动:

 包com.stevenschoen.test;

进口android.content.Intent;
进口android.os.Bundle;

进口com.actionbarsherlock.app.SherlockActivity;
进口com.actionbarsherlock.view.MenuItem;

公共类关于扩展SherlockActivity {
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.about);

        getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
        getSupportActionBar()setDisplayShowTitleEnabled(假)。
    }

    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){

            案例android.R.id.home:
                在操作栏中//应用程序图标点击;回家
                意图int​​entHome =新的意图(这一点,MainActivity.class);
                intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intentHome);
                返回true;

            默认:
                返回super.onOptionsItemSelected(项目);
        }
    }
}
 
使用TileMap系统搭建游戏场景

解决方案

发现我的问题的根源是在清单中我做了一个,而前一个变化:我加入这行:

 安卓launchMode =singleInstance
 

所以我的主要活动将不会重新启动。它更改为:

 安卓launchMode =singleTask
 

解决我的问题,我是能够消除所有的 onOptionsItemSelected 的东西。我知道有什么不对劲,那Android的应该已经能够处理这更好的,我是对的。检查清单:P

I use ActionBarSherlock (although I don't think it matters).

I have a Main activity and an About activity. I want the About activity to show the back-arrow by its logo, and do the proper animation and such. I don't know how to do this properly.

Currently, I have under onOptionsMenuItemSelected to launch the Main activity when the Up/Home button is pressed, but it's hacky and doesn't really work right. It plays the wrong animation, and handles multitasking poorly.

How do I set this up right?

Here's the part in my Main activity that launches the About:

Intent aboutIntent = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(aboutIntent);

Here's my About activity:

package com.stevenschoen.test;

import android.content.Intent;
import android.os.Bundle;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;

public class About extends SherlockActivity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:
                // app icon in action bar clicked; go home
                Intent intentHome = new Intent(this, MainActivity.class);
                intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intentHome);
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

解决方案

Found out the root of my problem was a change in the manifest I made a while ago: I added this line:

android:launchMode="singleInstance"

so my main activity wouldn't be relaunched. Changing it to:

android:launchMode="singleTask"

solved my problems, and I was able to remove all the onOptionsItemSelected stuff. I knew there was something wrong, that Android should have been able to handle this better, and I was right. Check the manifest :P