0

I have developed an application with both Activity and a background Service. My main Activity A is a Tab Host which starts the Service S and Activity B,C and D with tabs via Intent.

The Service S gets data from a remote database and stores it in the application's local database.

If there is a data from the Remote database, the Service S starts an Activity E with an Alert Box. Once i click the OK button of the Alert Box, the main Activity A(Tab Host) is opened.

Imagine the user is in Activity B and if the dialog box opens, after clicking OK button the user is switched to Activity A rather than Activity B. How can i go to Activity B(current active activity)?.

Part of Activity with Alert box.

public class Popup extends Activity{
int value = 0;

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

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null)
    {
        value = extras.getInt("key") ;     
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    if(value == 1){
        builder.setMessage(value +  "new task has been assigned").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                //mp.stop();

                Popup.this.finish();

            }
        }).show();
    }

    else {
        builder.setMessage(value + " " + "new tasks has been assigned").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {


            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                //mp.stop();

            }
        }).show();
    }}}
4

3 回答 3

0

在Activity A(TabHost)中,使用TabHostgetCurrentTab方法判断当前标签,并setCurrentTab设置当前标签。

于 2012-09-17T10:18:19.003 回答
0

您只需将代码更改为onClick Event 保留AlertBox它 Blankl 即可保持相同的活动

喜欢

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    if(value == 1){
        builder.setMessage(value +  "new task has been assigned").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {


        }
    }).show();
}

else {
    builder.setMessage(value + " " + "new tasks has been assigned").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {


        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    }).show();
于 2012-09-17T10:14:48.357 回答
0

当警报框出现时,您当前的活动将处于 onPause() 状态,因此需要覆盖 onPause() 方法并将一些布尔变量设置为 true,然后在 onResume() 方法中执行进一步的操作,就像这样

private boolean state=false;
    onPause(){
    state=true;
super.onPause();
}
onResume(){
  if(state==true){
//do your required stuff,for  example you want to stay in current activity(B)
       A.tabHost.setCurrentTab(here set B activity tab index);//declare tabHost variable as public static in activity A.
 super.onResume();

}

}

于 2012-09-17T10:34:15.117 回答