0

アプリでタブを使用しています。タブは正常に動作しています。タブをクリックすると、特定のアクティビティがタブの下に表示されます。タブの1つにメニュー機能があり、メニュータブをクリックすると、メニューのリストがタブの下に表示されます。メニュー リストをクリックすると、View を使用してタブの下にアクティビティが表示されました。問題はここから始まります。メニュー リストをクリックし、アクティビティが表示されると、電話の [戻る] ボタンを使用できません。戻るボタンをクリックすると、アプリが強制終了され、代わりにメニュー リストに戻ります。以下は私のコードです。

タブ アクティビティ

public class NfcSurveyActivity extends TabActivity {


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
                mTechLists);

        int profileCount = db.getContactsCount();
        if (profileCount <= 0) {
            Intent intent = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        int profileCount = db.getContactsCount();
        if (profileCount <= 0) {
            Intent intent1 = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(intent1);
        }

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            tabToDisplay = extras.getString("tab");
            if (tabToDisplay.equals("CAMERA")) {
                barcodeData = extras.getString("barcodeData");
            }
            extras.clear();
        }

        TabHost tabHost = getTabHost();

        // Home
        TabSpec tbspecHome = tabHost.newTabSpec("Home");
        tbspecHome.setIndicator("",
                getResources().getDrawable(R.drawable.tab_account_style));

        Intent iHome = new Intent(this, HomeActivity.class);
        tbspecHome.setContent(iHome);
        tabHost.addTab(tbspecHome);

        // History
        tabHost.addTab(tabHost
                .newTabSpec("Fun")
                .setIndicator("",
                        getResources().getDrawable(R.drawable.tab_fun_style))
                .setContent(
                        new Intent(this, NfcSurveyActivity.class)
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

        if (tabToDisplay != null && tabToDisplay.equals("REDEEM")) {
            if (barcodeData != null && barcodeData.length() > 0) {

                tabHost.addTab(tabHost
                        .newTabSpec("Camera")
                        .setIndicator(
                                "",
                                getResources().getDrawable(
                                        R.drawable.tab_redeem_style))
                        .setContent(
                                new Intent(this, NfcSurveyActivity.class)
                                        .addFlags(
                                                Intent.FLAG_ACTIVITY_CLEAR_TOP)
                                        .putExtra("autoLoadBarcodeData",
                                                barcodeData)));

            }

            else {
                tabHost.addTab(tabHost
                        .newTabSpec("Camera")
                        .setIndicator(
                                "",
                                getResources().getDrawable(
                                        R.drawable.tab_redeem_style))
                        .setContent(
                                new Intent(this, NfcSurveyActivity.class)
                                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));

            }

        } else {
            tabHost.addTab(tabHost
                    .newTabSpec("Camera")
                    .setIndicator(
                            "",
                            getResources().getDrawable(
                                    R.drawable.tab_redeem_style))
                    .setContent(
                            new Intent(this, NfcSurveyActivity.class)
                                    .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
        }

        // tabHost.setCurrentTab(2);

        tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = tabHost
                .getTabWidget().getChildAt(2).getLayoutParams().height + 19;

        // Search
        TabSpec tbspecSearch = tabHost.newTabSpec("Finder");
        tbspecSearch.setIndicator("",
                getResources().getDrawable(R.drawable.tab_finder_style));

        Intent iSearch = new Intent(this, NfcSurveyActivity.class);
        tbspecSearch.setContent(iSearch);
        tabHost.addTab(tbspecSearch);

        // Profile
        TabSpec tbspecProfile = tabHost.newTabSpec("Quit");
        tbspecProfile.setIndicator("",
                getResources().getDrawable(R.drawable.tab_quit_style));

        Intent iProfile = new Intent(this, NfcSurveyActivity.class);
        tbspecProfile.setContent(iProfile);

        tabHost.addTab(tbspecProfile);

        for (int i = 0; i <= 4; i++) {
            tabHost.getTabWidget()
                    .getChildTabViewAt(i)
                    .setBackgroundColor(
                            getResources()
                                    .getColor(android.R.color.transparent));

            if (i == 2) {
                tabHost.getTabWidget()
                        .getChildTabViewAt(i)
                        .setPadding(
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingLeft(),
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingTop(),
                                tabHost.getTabWidget().getChildTabViewAt(i)
                                        .getPaddingRight(), 20);
            }

        }

        if (tabToDisplay != null && tabToDisplay.length() > 0) {
            if (tabToDisplay.equals("CAMERA")) {
                tabHost.setCurrentTab(2);

            } else if (tabToDisplay.equals("HISTORY")) {
                tabHost.setCurrentTab(1);

            }

        }

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
                NfcSurveyConfiguration.SelectedTab = tabId;
            }
        });
    }

}

メニューアクティビティ

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_item_row);

initList();

// We get the ListView component from the layout
ListView lv = (ListView) findViewById(R.id.listView);
simpleAdpt = new SimpleAdapter(this, planetsList,
        android.R.layout.simple_list_item_1, new String[] { "planet" },
        new int[] { android.R.id.text1 });

lv.setAdapter(simpleAdpt);
// React to user clicks on item
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    public void onItemClick(AdapterView<?> parentAdapter, View view,
            int position, long id) {

        TextView clickedView = (TextView) view;
        String menu = clickedView.getText().toString();
        if (menu.equalsIgnoreCase("Point History")) {
            View view1 = getLocalActivityManager().startActivity(
                    "ReferenceName",
                    new Intent(getApplicationContext(),
                            PointsHistory.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                    .getDecorView();
            setContentView(view1);
        }
        else
        {
            View view1 = getLocalActivityManager().startActivity(
                    "ReferenceName",
                    new Intent(getApplicationContext(),
                            LoginActivity.class)
                            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                    .getDecorView();
            setContentView(view1);
        }

    }
});

}
4

3 に答える 3

0

このように使用できます

public void onBackPressed() {

    };

ユーザーが戻るキーを押したことをアクティビティが検出したときに呼び出されます。デフォルトの実装は単に現在のアクティビティを終了しますが、これをオーバーライドして、必要なことを行うことができます。

これを確認してください:https://stackoverflow.com/a/12171478/1168654

于 2013-03-22T09:26:21.893 に答える
0

onkeydown メソッドを追加してから、やりたいことを試すことができます

public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    //Handle the back button
    if( keyCode == KeyEvent.KEYCODE_BACK && isTaskRoot()) 
    {
//ur code here to do the required step either to exit the app or stay in there
}

return true;
}
于 2013-03-22T08:15:58.117 に答える
0

呼び出すアクティビティのリストを含む ActivityGroup を使用してみてください。 TabActivity で複数のアクティビティを処理する必要があります 。タブグループのアクティビティについてもお読みください

また

戻るボタンをオーバーライドして、アクティビティを終了する前に、開いているアクティビティのリストを維持し、背面で開始アクティビティを使用してアクティビティを開き、リストが終了したらタブアクティビティを終了することができます

また、これこのリンクを読んでください

于 2013-03-22T08:49:59.113 に答える