2

我有一个实现 TabActivity 的应用程序,但问题是当我单击后退按钮打开活动后,应用程序无法关闭,

我怎样才能完成这个 TabActivity ?

MainActivity.java

包 com.productdemo;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity {

TabHost tabhost;
TabSpec dashboard, product, customers, order, settings;

public final static int DASHBOARD = 1;
public final static int PRODUCT = 2;
public final static int CUSTOMER = 3;
public final static int ORDER = 4;
public final static int SETTINGS = 5;


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

    tabhost = getTabHost();

    TabHost.TabSpec spec;
    Intent intent;

    intent = new Intent().setClass(this, TabGroup1Activity.class);
    spec = tabhost.newTabSpec("Dashboard").setIndicator("Dashboard")
            .setContent(intent);
    tabhost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup2Activity.class);
    spec = tabhost.newTabSpec("Product").setIndicator("Product")
            .setContent(intent);
    tabhost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup3Activity.class);
    spec = tabhost.newTabSpec("Customer").setIndicator("Customer")
            .setContent(intent);
    tabhost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup4Activity.class);
    spec = tabhost.newTabSpec("Order").setIndicator("Order")
            .setContent(intent);
    tabhost.addTab(spec);

    intent = new Intent().setClass(this, TabGroup5Activity.class);
    spec = tabhost.newTabSpec("Settings").setIndicator("Settings")
            .setContent(intent);
    tabhost.addTab(spec);

    tabhost.setCurrentTab(1);


    int type = 0;
    if (getIntent().getExtras() != null) {
        if (getIntent().getExtras().containsKey("from")) {
            type = getIntent().getExtras().getInt("from");
            switch (type) {
            case DASHBOARD:
                tabhost.setCurrentTab(0);
            case PRODUCT:
                tabhost.setCurrentTab(1);
            case CUSTOMER:
                tabhost.setCurrentTab(2);
            case ORDER:
                tabhost.setCurrentTab(3);
            case SETTINGS:
                tabhost.setCurrentTab(4);
            default:
                tabhost.setCurrentTab(0);
            }
        }
    }
}

public void switchTabSpecial(int tab) {
    tabhost.setCurrentTab(tab);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private class MyView extends LinearLayout {
    ImageView iv;

    public MyView(Context c, int drawable, int drawableselec, String label) {
        super(c);

        iv = new ImageView(c);
        StateListDrawable listDrawable = new StateListDrawable();
        listDrawable.addState(SELECTED_STATE_SET, this.getResources()
                .getDrawable(drawable));
        listDrawable.addState(ENABLED_STATE_SET, this.getResources()
                .getDrawable(drawableselec));
        iv.setImageDrawable(listDrawable);
        iv.setBackgroundColor(Color.TRANSPARENT);
        iv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, (float) 0.0));
        iv.setPadding(0, 0, 0, 5);
        setGravity(Gravity.CENTER);

        addView(iv);
    }
}

@Override
public void onBackPressed() {
    this.finish();
}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="0dp" >
    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="0dp" >
    </FrameLayout>
    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0" />
    <TabWidget
        android:id="@+id/tabwidget"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0" />
</LinearLayout>

提前致谢...

4

4 回答 4

4

我只是定义 TabGroupActivity 来管理活动的子父关系,

TabGroup1Activity.java

package com.tabgroupdemo;  

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

public class TabGroup1Activity extends TabGroupActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startChildActivity("OptionsActivity", new Intent(this, DetailActivity.class));
}
}

DetailActivity.java

package com.tabgroupdemo;

import android.os.Bundle;

public class DetailActivity extends TabGroupActivity {

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

    // write your actual stuff here

}
}

TabGroupActivity.java

package com.tabgroupdemo;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;

/**
 * The purpose of this Activity is to manage the activities in a tab. Note:
 * Child Activities can handle Key Presses before they are seen here.
 * 
 * @author Eric Harlow
 */
public class TabGroupActivity extends ActivityGroup {

private ArrayList<String> mIdList;
String TAG = getClass().getSimpleName();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mIdList == null)
        mIdList = new ArrayList<String>();
}

/**
 * This is called when a child activity of this one calls its finish method.
 * This implementation calls {@link LocalActivityManager#destroyActivity} on
 * the child activity and starts the previous activity. If the last child
 * activity just called finish(),this activity (the parent), calls finish to
 * finish the entire group.
 */
@Override
public void finishFromChild(Activity child) {

    Log.v(TAG, "finish from child , mIdList size = " + mIdList.size());
    LocalActivityManager manager = getLocalActivityManager();
    int index = mIdList.size() - 1;

    if (index < 1) {
        finish();
        return;
    }
    manager.destroyActivity(mIdList.get(index), true);
    mIdList.remove(index);
    index--;
    String lastId = mIdList.get(index);
    Intent lastIntent = manager.getActivity(lastId).getIntent();
    Window newWindow = manager.startActivity(lastId, lastIntent);
    setContentView(newWindow.getDecorView());
}

/**
 * Starts an Activity as a child Activity to this.
 * 
 * @param Id
 *            Unique identifier of the activity to be started.
 * @param intent
 *            The Intent describing the activity to be started.
 * @throws android.content.ActivityNotFoundException.
 */
public void startChildActivity(String Id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(Id,
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        mIdList.add(Id);
        setContentView(window.getDecorView());
    }
}

/**
 * The primary purpose is to prevent systems before
 * android.os.Build.VERSION_CODES.ECLAIR from calling their default
 * KeyEvent.KEYCODE_BACK during onKeyDown.
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

/**
 * Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
 * all systems call onBackPressed().
 */
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
        return true;
    }
    return super.onKeyUp(keyCode, event);
}

/**
 * If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and
 * add this method.
 */
public void onBackPressed() {
    int length = mIdList.size();
    if (length > 1) {
        Activity current = getLocalActivityManager().getActivity(
                mIdList.get(length - 1));
        current.finish();
    } else {
        finish();
    }
}
}

在 TabGroupActivity.java 方法中,最后一行是 finish() 在 else 部分导致 TabActivity 完成。

于 2013-04-08T04:36:09.467 回答
2

您可以使用以下代码..

intent = new Intent().setClass(this, TabGroup2Activity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

spec = tabhost.newTabSpec("Product").setIndicator("Product").setContent(intent);

tabhost.addTab(spec);

此代码在每个选项卡意图对象的主要活动的 onCreate() 方法中使用...

于 2013-04-07T05:31:00.800 回答
0

我不确定为什么活动不会关闭,但你不应该需要

@Override
public void onBackPressed() {
    this.finish();
}

因为当按下返回按钮时,Android 会自动完成一个活动。

于 2012-12-22T21:21:18.083 回答
0

试试这个:

@Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
                this.finish();
    }
于 2013-01-03T10:09:21.393 回答