According to my requirements i need to implements tabs for my apps. Such that each tabs associated with multiple activities. Like: Tab1, Tab2, Tab3...etc. Tab1: Activity1,Activity2,Activity3....etc. I want to launch activities in such order: eg: Tab1 -> Activity1 -> Activity2. This is fine i have doen it.
But if i press device back key from Activity2 then it is coming to home screen but it should come activity1. But if i am going back through ui component(like back button) then it is working fine.
Below are my TabGoupActivity and Activity 1, Activity2 implementation.
TabGruopActivity:-
public class TabGroupActivity extends ActivityGroup {
private ArrayList<String> mIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null) mIdList = new ArrayList<String>();
}
@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.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
setContentView(newWindow.getDecorView());
}
public void startChildActivity(String Id, Intent intent) {
Window window = null;
try{
window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP) );
}catch(Exception e){
System.out.println(e.toString());
}
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
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);
}
public void onBackPressed () {
int length = mIdList.size();
if ( length >= 1) {
Activity current = getLocalActivityManager().getActivity(mIdList.get(length-1));
current.finish();
}
}
}
TabGroup1Activity: Which extends TabGroupActivity(Above Activity):-
public class TabGroup2Activity extends TabGroupActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("Activity2", new Intent(this, DemoActivity2.class));
}
}
Activity1:-
public class DemoActivity2 extends Activity {
Context context;
protected static LocalActivityManager mLocalActivityManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity2);
context = this;
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context, DemoActivity3.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("Activity3", intent);
}
});
Button button2 = (Button) findViewById(R.id.Button02);
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
ScrollableTabActivity.ACTION_CHANGE_TAB);
intent.putExtra(ScrollableTabActivity.CURRENT_TAB_INDEX, 0);
sendBroadcast(intent);
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
Activity2:-
public class DemoActivity3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_activity3);
Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabGroupActivity p开发者_JAVA技巧arentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
Can any one tell to me why device back button is not working?
I use TabHost and TabActivity but this must work for your need to. Hope you can use it :)
put this listener inside your:
public class TabGroupActivity extends ActivityGroup
Listener:
myListener = new MyListener();
protected class MyListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.test.switchtabs")) {
int res = intent.getIntExtra("switch",10);
if(res == 0){
tabHost.getTabWidget().getChildAt(0).performClick();
}else if(res == 1){
tabHost.getTabWidget().getChildAt(1).performClick();
}else if(res == 2){
tabHost.getTabWidget().getChildAt(2).performClick();
}
}else if (intent.getAction().equals("com.test.sendnow")) {
sendTheFiles();
}
}
}
Inside your TabGroupActivity register/unregister deepening of your needs, the custom listener:
@Override
protected void onResume() {
super.onResume();
if (!MyListenerIsRegistered) {
IntentFilter intentFilterSwitchTabs = new IntentFilter("com.your.package.switchtabs");
registerReceiver(myListener, intentFilterSwitchTabs);
MyListenerIsRegistered = true;
}
}
@Override
protected void onPause() {
super.onPause();
if (MyListenerIsRegistered && myListener != null) {
unregisterReceiver(myListener);
MyListenerIsRegistered = false;
}
}
put this in your AndroidManifest.xml
<activity android:name="com.your.package.TabGroupActivity">
<intent-filter>
<action android:name="com.your.package.switchtabs" />
</intent-filter>
</activity>
From any activity send a Broadcast like this to change tabs,
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.etx_addtext_drawtext:
case R.id.button_back:
Intent i = new Intent();
i.putExtra("switch", 1);
i.setAction("com.your.package.switchtabs");
sendBroadcast(i);
break;
}
}
精彩评论