根据我的要求,我需要为我的应用实现标签。这样每个标签与多个活动相关联。如:Tab1,Tab2,Tab3 ......等。 Tab1:Activity1,Activity2,Activity3 ....等。我想以这样的顺序发起活动: 例如:Tab1 - >活动1 - >活性2。这很好我已经做过了。
但是,如果我从Activity2按下设备返回键,那么它将进入主屏幕,但它应该来自activity1。但是,如果我通过ui组件(如后退按钮)返回,那么它工作正常。
以下是我的TabGoupActivity和Activity 1,Activity2实现。
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:扩展了TabGroupActivity(活动以上): -
public class TabGroup2Activity extends TabGroupActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startChildActivity("Activity2", new Intent(this, DemoActivity2.class));
}
}
活性1: -
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);
}
}
活性2: -
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 parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
任何人都可以告诉我为什么设备后退按钮不起作用吗?
答案 0 :(得分:0)
我使用TabHost和TabActivity但这必须适合您的需要。 希望你能用它:)
将此侦听器放入您的:
public class TabGroupActivity extends ActivityGroup
监听器:
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();
}
}
}
在TabGroupActivity注册/取消注册深化您的需求,自定义监听器:
@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;
}
}
将其放入AndroidManifest.xml
<activity android:name="com.your.package.TabGroupActivity">
<intent-filter>
<action android:name="com.your.package.switchtabs" />
</intent-filter>
</activity>
从任何活动中发送此类广播以更改标签
@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;
}
}