简体   繁体   中英

Android Intent Activity Callback

I'm developing an Android module that basically consists of a custom View third parties can just drop into any of their activities. This module includes a configuration activity (based on PreferenceActivity).

Given that you can't start an activity from a View, the module calls 'openConfig(Intent intent)' on the activity it is displayed in. This activity acts as a delegate for the module.

That works fine thus far (though I'd really like the module to handle everything internally, no delegate methods required, but I reckon that just isn't possible). However, I need some sort of callback from the preference activity to the module, so that the module will get notified when the settings have been changed.

I was thinking of just adding the module's main class as a delegate to the preference activity;

ConfigActivity a = new ConfigActivity();
a.testVar = "testtest";
Intent intent = new Intent(getContext(), a.getClass());
delegate.handleConfigAction(intent); //

However, this test with just a simple String (instead of an interface) showed, that the String wouldn't get set after the activity has been started.

Second thought was to use 'putExtra()' on the intent, but that doesn't really suit the use case as the delegate I'd like to put there really is a View and not a serializable data object.

Are there any ways for me to handle this internally? I am aware of the 'onActivityResult()' function ( http://developer.android.com/training/basics/intents/result.html ), but that would mean that the third party developer using my module would need to handle this, something that needs to be avoided for obvious reasons.

Any help would be highly appreciated!

EDIT: FINAL SOLUTION In the end I've changed the module from View to Fragment, which now works much better with handling "child" activities and such. When starting an Activity from a Fragment, the 'onActivityResult' function works beautifully to accomplish the task at hand.

You can start an Activity from a View using its Context as in:

@Override
public void onClick() {
    Intent intent = new Intent(getContext(), ConfigActivity.class);
    intent.putExtra("testVar", "testtest");
    getContext().startActivity(intent);
}

You could use the onWindowVisibilityChanged() method to read the configuration that would be set in the ConfigActivity to make your View change its behavior.

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