mActivity从Fragment.onAttach()不保留 [英] mActivity from Fragment.onAttach() not retained

查看:419
本文介绍了mActivity从Fragment.onAttach()不保留的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装有一个RxJava观测/用户。可观察到的是MainActivity创建。该用户是 android.support.v4.app.Fragment 称为MyFragment。可观察到的从RESTful服务中获取数据,并在设备上的SQLite数据库存储数据。这工作。当其工作完成,这需要4秒或5秒,这样MyFragment已经处理的onCreate() onCreateView()等,用户,MyFragment,通过其实施 onNext()方法通知(这工作),然后从SQLite的数据库中读取数据(这也适用)和是应该填充视图(这不起作用)。

I have setup a Observable/Subscriber with RxJava. The Observable is created in MainActivity. The Subscriber is a android.support.v4.app.Fragmentcalled MyFragment. The Observable gets data from RESTful service and stores the data in a SQLite db on the device. This works. When its work is done, which takes 4 or 5 seconds such that MyFragment has already processed onCreate(), onCreateView() etc., the subscriber, MyFragment, is notified via its implemented onNext() method (this works) and is then reads data from the SQLite db (this also works) and is the supposed to populate a view (this does not work).

问题是,我的类成员mActivity是在方法无效 loadDataFromSQLite()。我觉得我学习的教训是,没有任何一个片段可以在片段类(方法外完成从 onAttach()的onDestroy()

The problem is that my class member mActivity is null in the method loadDataFromSQLite(). The lesson I think I am learning is that nothing with a Fragment can be done outside of methods in the Fragment class (from onAttach() to onDestroy())

既然如此,我该怎么做呢?

That being the case, how do I do this?

请参阅下面的code片段:

See below for code snippets:

MyFragment.java

MyFragment.java

public class MyFragment extends Fragment implements Observer<String> { 

    private Activity mActivity;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
    }

    ...

    @Override
    public void onNext(String string) {
        loadDataFromSQLite();

    }

    public void loadDataFromSQLite() {
        <get data from SQLite>
        // now want to populate view
        // mActivity is null
        mTableLayout = (TableLayout) mActivity.findViewById(R.id.fragment_table_layout);
    }
}    

fragment_table_layout.xml

fragment_table_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/table_wrapper"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        tools:ignore="UselessParent">

        <TableLayout
            android:id="@+id/fragment_table_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <!--  fill in data in MyFragment.java -->
        </TableLayout>

    </ScrollView>
</RelativeLayout>

编辑:

我要补充的我也打过电话 getActivity() mActivity ,但它返回null

I should add the I also tried calling getActivity() in place of mActivity, but it returns null

推荐答案

显然这行是有问题的:

mTableLayout = (TableLayout) mActivity.findViewById(R.id.fragment_table_layout);

这意味着你投不属于该片段,该分段的视图。一个片段的所有观点管道(包括发现TableLayout)应 Fragment.onCreateView 来完成。之后你可以在其他地方使用他们引用的片段,像这样的:

This means you are casting a view that doesn't belong to that Fragment, in that Fragment. All view plumbing of a Fragment (including finding that TableLayout) should be done in Fragment.onCreateView. Later you can use their references elsewhere in the Fragment, like this:

TableLayout mTableLayout;

@Override
public View onCreateView(LayoutInflater inflater, ...) {
   View view = inflater.inflate(R.layout.fragment_layout, container, false);
   mTableLayout = (TableLayout) view.findViewById(R.id.fragment_table_layout);
   ...
   return view; 
}

另一件事是,你不能配置两个中的一个过程,只是因为你希望它需要更长的时间来更新另一个。这是不可靠的,这是你通过调用REST API来更新你不知道已经创造了一个片段做什么。您可以通过调用强制片段创建FragmentManager.executePendingTransactions(),但你仍然需要prepare的情况下,当片段已经一去不复返了REST响应到达。

Another thing is that you cannot configure one of two processes to update the other one simply because you expect it to take longer. This is unreliable and this is what you do by calling a REST API to update a fragment that you're not sure has been created. You can force the creation of a Fragment by invoking FragmentManager.executePendingTransactions() but you still need to prepare for the case when the fragment is already gone when REST response arrives.

同样重要的是要注意保持一个活动的引用是一个坏主意。你应该总是在片段使用 getActivity 。如果在任何时候返回null,则意味着该片段被固定到尚未创建或已毁的活动。

It is also important to note that keeping a reference to an Activity is a bad idea. You should always use getActivity in a Fragment. If at any point it returns null then it means the Activity the Fragment is anchored to is not yet created or already destroyed.

这篇关于mActivity从Fragment.onAttach()不保留的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆