简体   繁体   中英

Communication between running activities?

I'm new to android and is currently developing a quest/tresurehunt application. It is a prototype and is going to be evaluated in the field and i would therefore like to implement some sort of cheat to skip checkpoints if the application should crash during the tests.

My application work the way that when you select a quest a activity will be loaded to handle all checkpoints and the user location(ActivityAdapter.java). This activity will open the diffrent navigation tool activities and pass information to them based on the next checkpont using intent. I have implemented a long press event in the app i would like to activate the skip/cheat. My problem is that i can't figure out how to do this.

Location changed event of ActivityAdapter.java:

public void onLocationChanged(Location location) {

    location.distanceBetween(location.getLatitude(), location.getLongitude(), lat, lng, dist);
    if (dist[0]<= 10) {
        if (cid == checkpoints.size()) {
             Intent intent = new Intent(ActivityAdapter.this, SuccessActivity.class);
             intent.putExtra("title", checkpoints.get(cid).get("title"));
             startActivity(intent);
        } else {
             new loadAndStartQuest().execute();
        }   
    }
}

loadAndStartQuest() just find the next checkpoint and start the right activity (navigationtool). I have tryed creating a object of the ActivityAdapter and set a variable and add it to the the if statement which did not work. I guess it is because it will create another instance of the activity and not affect the current/running one. So how would you communicate between two running activities?

To send data from Activity1 to running Activity2, you must pass through the next steps:

  1. In Manifest.xml set launch mode "singleTask" for Activity2

    <activity android:name="Activity2" android:launchMode="singleTask">

  2. Put extras and start Activity2 from Activity1 (if Activity2 was started before, intent will be sended to existing instance).

     Intent intent = new Intent(getContext(), Activity2.class); intent.putExtra("CHECK_POINT", checkPointData); startActivity(intent) 
  3. Override method onNewIntent() in Activity2

     protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); onNewCheckPoint(); } private void onNewCheckPoint(){ Intent intent = getIntent(); Bundle extras = intent.getExtras(); //in this moment, you can process data, like you want. } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM