简体   繁体   English

当AsyncTask完成活动时,调用片段方法

[英]Call fragment method when AsyncTask finish on activity

I have an app with a Navigation Drawer Activity which is calling some fragments on it. 我有一个带有“导航抽屉活动”的应用程序,它在其上调用了一些片段。

In one of them, I have a Google Map integration, where retrieving from SharedPreferences, I show some markers and stuff. 在其中之一中,我具有Google Map集成,其中从SharedPreferences检索,其中显示了一些标记和内容。

On the Activity, I have an AsyncTask to update the data when clicking an action bar button. 在活动上,我有一个AsyncTask可以在单击操作栏按钮时更新数据。 It takes the new data into the SharedPreferences. 它将新数据放入SharedPreferences中。

I'm having trouble finding a way to call again the paintmap() method from the FragmentMap which is the one that takes the SharedPreferences data and paint the map with the new markers and so on. 我在寻找一种方法来从FragmentMap再次调用paintmap()方法时遇到麻烦,FragmentMap是一种使用SharedPreferences数据并使用新标记绘制地图的方法,依此类推。

I have tried OnSharedPreferenceChangeListener with no result. 我尝试了OnSharedPreferenceChangeListener,但没有结果。

Also tried getting the actual fragment with findFragmentByTag but I'm not able to call the method. 还尝试使用findFragmentByTag获取实际片段,但无法调用该方法。

So, what's the better way to tell the fragment that the data has been updated and need to redraw the map? 那么, 告诉片段数据已更新并且需要重绘地图的更好方法是什么?

Thanks in advance. 提前致谢。

Edit 1: Some things I have tried: 编辑1:我尝试过的一些事情:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_map, container, false);

    txtUpdateInfo = (TextView) view.findViewById(R.id.last_update_info);


    initilizeMap(); // This one includes paintMap at the end. It creates the map.
Check this pastebin for more: http://pastebin.com/DPPB7FiK


    SharedPreferences.OnSharedPreferenceChangeListener spChanged = new SharedPreferences.OnSharedPreferenceChangeListener() {

        @Override
        public void onSharedPreferenceChanged(
                SharedPreferences sharedPreferences, String key) {

            paintMap();
        }

    };

    return view;
}

Also tried, on MainActivity AsyncTask's postExecute, to retrieve the fragment: 还尝试在MainActivity AsyncTask的postExecute上检索该片段:

FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentByTag("MAP");

f.paintMap(); // This gives me error and I cannot compile

Also, in the same place: 另外,在同一位置:

 FragmentMap f = new FragmentMap();
 f.paintMap(); 

This last one compiles but gave me NullPointer to everything on the fragment... not working neither. 这最后一个编译了,但是给了我NullPointer处理片段上的所有内容……都不起作用。

The problem with this code... 这段代码的问题...

FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentByTag("MAP");

f.paintMap(); // This gives me error and I cannot compile

...is that you've declared f as a Fragment and the Android Fragment class doesn't have a method called paintMap() . ...就是您已将f声明为Fragment ,而Android Fragment类没有名为paintMap()的方法。 You need to declare it as FragmentMap and cast the return of findFragmentByTag as follows... 您需要将其声明为FragmentMap findFragmentByTag如下所示findFragmentByTag的返回值...

FragmentManager fm = getFragmentManager();
FragmentMap f = (FragmentMap) fm.findFragmentByTag("MAP");
f.paintMap();

The call to f.paintMap() should work then. 然后,对f.paintMap()的调用应该起作用。

Also (for reference) the following will never work... 另外(仅供参考)以下内容将永远无法使用...

FragmentMap f = new FragmentMap();
f.paintMap(); 

The reason is that like an Activity a Fragment has a life-cycle which means it isn't fully initialised until it is used in a FragmentTransaction and has gone through its life-cycle methods such as onAttach , onCreateView , onResume etc. 原因是,像Activity一样, Fragment具有生命周期,这意味着直到将其用于FragmentTransaction并经历了其生命周期方法(例如onAttachonCreateViewonResume等) onAttach ,它才被完全初始化。

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

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