简体   繁体   English

Android Intent活动回调

[英]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. 我正在开发一个基本上包含自定义View的Android模块,第三方可以直接参与其任何活动。 This module includes a configuration activity (based on PreferenceActivity). 该模块包括配置活动(基于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. 鉴于您无法从View启动活动,模块将在显示的活动上调用“ openConfig(Intent intent)”。此活动充当模块的委托。

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. 但是,仅使用一个简单的String(而不是接口)进行的测试显示,活动开始后不会设置String。

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. 第二个想法是在意图上使用'putExtra()',但这并不完全适合用例,因为我想放置的委托实际上是一个View而不是一个可序列化的数据对象。

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. 我知道'onActivityResult()'函数( http://developer.android.com/training/basics/intents/result.html ),但这意味着使用我的模块的第三方开发人员需要处理此问题,出于明显的原因需要避免的事情。

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. 从片段开始活动时,“ onActivityResult”功能可以很好地完成手头的任务。

You can start an Activity from a View using its Context as in: 您可以使用其ContextView启动Activity ,如下所示:

@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. 您可以使用onWindowVisibilityChanged()方法读取将在ConfigActivity设置的配置,以使View更改其行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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