开发者

Synchronous Android activities

Ive made mis-leading topic in my last question, so i open this new question to clear what I realy want. sorry for the inconvenience.

I wanna run two system(Android) activities one after another in specific order from my main activity.

now as we know, startActivity is an asynchronous operation, so i cant keep on a specific order.

so i thought maybe I should try to do it with dialogBox in the middle but also running a dialogBox is an asynchronous.

now as开发者_运维百科 i said the activities which i try to run are Android activities, so i cant even start them with startActivityForResult (or mybe i can, but i dont get any result back to my main(calling) activity) Any tricks how could i manage with this issue?

Some code:

first activity:

      Intent intent = new Intent(); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      intent.setAction(Settings.ACTION_APPLICATION_SETTINGS); 
      startActivity(intent);    

second activity:

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.setDataAndType(Uri.fromFile(tmpPackageFile 
        .getAbsoluteFile()), 
        "application/vnd.android.package-archive"); 
startActivity(intent); 

as you can see, i dont have any access to those activites, i can just run thire intents from my main activity.


Since you can't have 2 activities running at the same time, start the first, and then start the second once the first returns.

One way of tracking this is to use the startActivityForResult method, it should allow you to control the order the activities are created.


Try this...

Intent intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
                startActivityForResult(intent, 0);

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {

                // start the other activity

            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }


public class MainActivity extends AppCompatActivity

{
    String msg="Android";

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(msg,"1:application created");

    }

    protected void onStart() {
        super.onStart();
        Log.d(msg,"2 The onStart() ");
    }
    protected void onResume() {
        super.onResume();
        Log.d(msg, "3 The onResume() ");
    }
    protected void onPause() {
        super.onPause();
        Log.d(msg, "4 The onPause() ");
    }
    protected void onStop() {
        super.onStop();
        Log.d(msg, " 5 The onStop() ");
    }

    public void onDestroy() {
        super.onDestroy();
        Log.d(msg, "6 The onDestroy()");
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜