Question

(I know that Activity Groups and Tabs have been deprecated. I cannot redesign my app at the moment, so I want to finish it with the current design if possible.)

I have a TabActivity with three tabs, A, B, and C. Tab C is an ActivityGroup with two child activities, C1 and C2. C2 is always called from C1 and dismissed by setting the C1 View and restarting the C1 intent. All this works fine. My problem is that if the user selects the A or B tab while in the C2 activity, I want the C2 activity to cancel. The next time that the C Tab is selected, the C1 activity should display. Same thing if the user exits the application and re-enters it.

I have implemented the OnTabChangeListener(), so I know when a new tab is selected, and I can even tell if the C2 tab is active (or was before the tab changed). My question is how to cancel the C2 activity so that when the C tab is selected, the default C1 activity is started instead of returning to the previously active C2 tab.

Was it helpful?

Solution

Simply you can make use of your onPause() in Tab C and override it with the same code what you could have used of getting back from c2 to c1. Since onPause() will be called only when you come out of that particular tab, your activity group will be restored at the background, and when you get back to that Tab C you can see your C1 at the first place.

OTHER TIPS

I think you can use broadcast sender and receiver for that purpose. In onpause create a broadcast receiver. I am suggesting this because if you want to finish c2 from any other place also then you can do it easily by sending broadcast. Use this code :

BroadcastReceiver broadcast_receiver = null; // DECLARE IT IN YOUR CLASS.

// WRITE BELOW CODE IN onPause OF C2 ACTIVITY.
broadcast_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("finish_c2")) {
            unregisterReceiver(broadcast_receiver);
            finish();
        }
    }
};
    registerReceiver(broadcast_receiver, new IntentFilter("finish_c2"));

To finish your activity just send broadcast from any place.

//WRITE THE BELOW CODE WHERE YOU WANT TO FINISH C2 ACTIVITY.
Intent intent = new Intent("finish_c2");
sendBroadcast(intent);

hope it will help you ask if any problem occurs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top