parentActivityName强制onCreate

时间:2016-01-20 19:54:36

标签: java android

在我的Android清单中,我指定了parentActivityName,以便利用活动中显示的后退按钮。

我发现这会重新调用上一个活动的创建,我会在其中进行多次网络调用并设置一个回收站视图小部件。

这会导致屏幕完全刷新,我想避免这种情况并处理recyclerview以及要显示和删除的新内容,以确保用户流不会被震动。

我目前在onCreate中设置我的应用程序

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container_list);

        final SwipeRefreshLayout swiperefreshContainerListRecyclerView = (SwipeRefreshLayout) this.findViewById(R.id.swiperefresh_container_list_recyclerview);
    swiperefreshContainerListRecyclerView.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    refreshContainerList();
                }
            }
    );

    // Get a handle on our RecyclerView for interactin and setup
    containerListRecyclerView = (RecyclerView) findViewById(R.id.container_list_recyclerview);

    // Grab a new LayoutManager
    containerListLayoutManager = new LinearLayoutManager(this);

    // Grab a new adapter
    List<ContainerModel> containers = new ArrayList<>();
    containerListRecyclerAdapter = new ContainerListRecyclerViewAdapter(containers, this);

    // Better performance as the size of our RecyclerView does not change
    containerListRecyclerView.setHasFixedSize(true);

    // Attach our LayoutManager to our RecyclerView
    containerListRecyclerView.setLayoutManager(containerListLayoutManager);

    // Wire up adapter for RecyclerView
    containerListRecyclerView.setAdapter(containerListRecyclerAdapter);

    cAdvisorService = new CAdvisorService();

    cAdvisorService.fetchDataFromService(context, containerListRecyclerAdapter);

    System.out.println("Running onCreate!");
}

我的cAdvisorService中已经有逻辑来处理删除和添加项目到RecyclerView。

我如何处理每次调用onCreate的事实,强制我的RecyclerView和cAdvisorService的新实例?

1 个答案:

答案 0 :(得分:1)

通过在Android清单中将您的父活动声明为

,可以实现此目的
android:launchMode="singleTop"

,请参阅:How can I return to a parent activity correctly?