热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在方向改变时处理匕首组件-Handlingdaggercomponentonorientationchange

Assumingwhathavebeensaidhere,itsthedevelopersresponsibilitytokeepthecomponentinstanc

Assuming what have been said here, it's the developer's responsibility to keep the component instance in order to implement their own scope logic (since a scoped method will return the same instance for a given component).

假设这里已经说过,开发人员有责任保留组件实例以实现自己的范围逻辑(因为范围方法将为给定组件返回相同的实例)。

What's the clean way to keep this component reference through the activity lifecycle ?

通过活动生命周期保持此组件引用的干净方法是什么?

Example : You're implementing the MVP pattern, so you need a Presenter within your Activity. This Presenter can do a network operation to download items. When the device rotate, your Activity is being destroyed and recreated but you would like to keep the network operation going and just get back the pre-rotation presenter.

示例:您正在实施MVP模式,因此您需要在Activity中使用Presenter。此演示者可以执行网络操作来下载项目。当设备旋转时,您的Activity将被销毁并重新创建,但您希望保持网络运行,并且只需返回预旋转演示者。

Scoping the Component providing the Presenter with a custom PerActivity scope is the solution, so you have to keep the Component instance through this rotation to get injected the same instance of Presenter as the first launch of the Activity.

为提供Presenter的组件定义一个自定义PerActivity范围是解决方案,因此您必须通过此循环保持Component实例,以便在首次启动Activity时注入相同的Presenter实例。

How can we deal with this ? I thought of a kind of Component Cache (like a HashMap ?) that could be provided by an Application Component living inside the Application class.

我们怎么处理这个?我想到了一种组件缓存(如HashMap?),它可以由生活在Application类中的Application Component提供。

2 个解决方案

#1


0  

You can see the implementation of ribot/android-boilerplate showcase app. The solution they chose is to have a static Map inside the BaseActivity, from which all activities extend from.

你可以看到ribot / android-boilerplate showcase app的实现。他们选择的解决方案是在BaseActivity中包含一个静态Map ,所有活动都从中扩展。

public class BaseActivity extends AppCompatActivity {

    private static final AtomicLong NEXT_ID = new AtomicLong(0);
    private static final Map sCompOnentsMap= new HashMap<>();

    private ActivityComponent mActivityComponent;
    private long mActivityId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create the ActivityComponent and reuses cached ConfigPersistentComponent if this is
        // being called after a configuration change.
        mActivityId = savedInstanceState != null ?
                savedInstanceState.getLong(KEY_ACTIVITY_ID) : NEXT_ID.getAndIncrement();

        ConfigPersistentComponent configPersistentComponent;
        if (!sComponentsMap.containsKey(mActivityId)) {
            // Creating new component
            cOnfigPersistentComponent= DaggerConfigPersistentComponent.builder()
                    .applicationComponent(BoilerplateApplication.get(this).getComponent())
                    .build();
            sComponentsMap.put(mActivityId, configPersistentComponent);
        } else {
            // Reusing component
            cOnfigPersistentComponent= sComponentsMap.get(mActivityId);
        }
        mActivityCompOnent= configPersistentComponent.activityComponent(new ActivityModule(this));
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong(KEY_ACTIVITY_ID, mActivityId);
    }

    @Override
    protected void onDestroy() {
        if (!isChangingConfigurations()) {
            // Activity is finishing, removing the component
            sComponentsMap.remove(mActivityId);
        }
        super.onDestroy();
    }

    ...

}

#2


0  

Network can work with app context. This is how i would design Applicationcomponent with appscope Now i would have this created on application layer ApplicationComponent Shouod take context as external dependency

网络可以使用应用程序上下文。这就是我用appscope设计Applicationcomponent的方法现在我将在应用层上创建这个应用程序组件Shouod将上下文作为外部依赖

Next is activitycomponent extending on activitymodule with peractivityscope ..depending on applicationcomponet

接下来是使用peractivityscope在activitymodule上扩展的activitycomponent。取决于applicationcomponet

In my every activity i would create activityComponet by providing it applicationcomponet This applicationcomponet can be accessed via. Activity.getapplication().getapplicationcomponent()

在我的每个活动中,我将通过提供applicationcomponet来创建activityComponet。可以通过访问此应用程序组件。 Activity.getapplication()。getapplicationcomponent()

Here make sure your applicationmodule providing network method has appscope If thats the case u should get same network even on app rorate.

这里确保你的applicationmodule提供网络方法有appcope如果是这样的情况你应该得到相同的网络甚至在应用程序rorate。

Look for GitHubapplication sample will post the link in next edit.

查找GitHubapplication示例将在下次编辑中发布链接。

Also it will he worth while to look at livedata (out of context for this question)

同样值得看看livingata(这个问题的上下文)


推荐阅读
author-avatar
北若久
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有