如何从ActivityGroup中的子活动返回父活动?

时间:2014-04-04 10:38:00

标签: android

我已经实现了一个TabActivity,它有4个标签,当我转到此标签时,其中一个标签是ParentActivity,当我点击此按钮时,有一个按钮显示Go To Child我'能够按照以下方式参加儿童活动:

 /****************** ON BUTTON CLICK ******************/
 Intent childView = new Intent(v.getContext(), ChildActivity.class);
 replaceContentView("child_view", childView );
 //ParentActivity.this.finish();
 /****************************************************/

 public void replaceContentView(String activityID, Intent activityIntent) {
 View view = getLocalActivityManager().startActivity(activityID,activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
 this.setContentView(view);
 }

现在,当我点击ChildActivity中显示ParentActivity的按钮时,我想回到Go To Parnt。我怎么做,我尝试了很多例子,但我没有得到它。我希望我的标签必须在所有活动中。我已经包含了示例活动图片。enter image description here

1 个答案:

答案 0 :(得分:1)

启动Child Activity,如:

 Intent edit = new Intent(getParent(), Contact.class);
 TabGroupActivity parentActivity = (TabGroupActivity) getParent();
 parentActivity.startChildActivity("Contact", edit);

然后点击Child Activity点击按钮,点击:

back_btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        TabGroupActivity parentActivity = (TabGroupActivity) getParent();
        parentActivity.onBackPressed();

        }
    });

onBackPressed()上实施TabGroupActivity,如:

@Override
public void onBackPressed() {
    try{
        Log.i("TabGroup", "Current on BackPressed called: ");
        int length = mIdList.size();
        Log.i("TabGroup", "Current ArrayList Size(): "+length);
        if (length > 1) {

            Activity current = getLocalActivityManager().getActivity(
                    mIdList.get(length - 1));

            Log.i("TabGroup", "Current Last Activity"+current);

            current.finish();
        } else {
            MakeExitDialog();
        }
    }catch(Exception e){
        e.printStackTrace();
    }

}

并在startChildActivity(....)

中添加TabGroupActivity
public void startChildActivity(String Id, Intent intent) {
    // LocalActivityManager manager = getLocalActivityManager();
    int count = mIdList.size() - 1;
    for (int i = count; i >= 0; i--) {
        // System.out.println("Activity "+i+":::"+mIdList.get(i));
    }

    // System.out.println("Activity Started");
    Window window = getLocalActivityManager().startActivity(Id,
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        mIdList.add(Id);
        setContentView(window.getDecorView());
    }
}

更新:让您的主TabGroupActivity赞成:

  public class TabGroupActivity extends ActivityGroup {

private ArrayList<String> mIdList;
public static TabGroupActivity tabgrp;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mIdList == null)
        mIdList = new ArrayList<String>();
        int length = mIdList.size();
        Log.i("TabGroupActivity", "ArrayList Size(): "+length);
}



@Override
public void finishFromChild(Activity child) {

    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());
}


public void startChildActivity(String Id, Intent intent) {
    // LocalActivityManager manager = getLocalActivityManager();
    int count = mIdList.size() - 1;
    for (int i = count; i >= 0; i--) {
        // System.out.println("Activity "+i+":::"+mIdList.get(i));
    }

    // System.out.println("Activity Started");
    Window window = getLocalActivityManager().startActivity(Id,
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        mIdList.add(Id);
        setContentView(window.getDecorView());
    }
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        return true;
    }
    return super.onKeyDown(keyCode, event);
}


@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
        return true;
    }
    return super.onKeyUp(keyCode, event);
}


@Override
public void onBackPressed() {
    try{
        Log.i("TabGroup", "Current on BackPressed called: ");
        int length = mIdList.size();
        Log.i("TabGroup", "Current ArrayList Size(): "+length);
        if (length > 1) {

            Activity current = getLocalActivityManager().getActivity(
                    mIdList.get(length - 1));

            Log.i("TabGroup", "Current Last Activity"+current);

            current.finish();
        } else {

        }
    }catch(Exception e){
        e.printStackTrace();
    }

}

 }