How can I run something Async in OncreateView?

我只是一个虾纸丫 提交于 2019-12-22 13:07:36

问题


I have a problem with my app. Firstly, I made two Tabs using fragments which inflates an activity. The tabs implemented is working fine. Secondly i have displayed the XAML right.

However, I now need to run something asynchronously in OnCreateView in the Fragment. How can i manage this without getting errors?

Any help is greatly appreciated! Thanks

Here is my Fragment:

public class FragmentSettings : SupportFragment
{
    private Button mBtnOk;

    public FragmentSettings(Context context)
    {
        mContext = context;
    }

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        //Inflate the fragment XML
        View view = inflater.Inflate(Resource.Layout.FragmentSettings, container, false);

        //Grab the butten from the inflated fragment
        mBtnOk = view.FindViewById<Button>(Resource.Id.mBtnOk);
        mBtnOk.Click += (object sender, EventArgs e) =>
        {
            //DO stuff
        };
        //=====================================
        //Want to do something async stuff here
        //=====================================

        return view;
    }
}

回答1:


This did the trick for me, thanks for all the answers!

    public override async void OnActivityCreated(Bundle savedInstanceState)
    {
        base.OnActivityCreated(savedInstanceState);


        await LoadData();
    }
    private async Task LoadData()
    {
     //My async method 
    }



回答2:


For example you can simply run

AsyncTask

Just put there something like this:

 new AsyncTask<Void, Void, Void>() {
     @Override
     protected Void doInBackground(Void... voids) {
         //do your work here
         return null;
     }
 }.execute();



回答3:


I would just use a Backgroundworker, if you need stuff done in the background:

BackgroundWorker bworker;

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    //Inflate the fragment XML
    View view = inflater.Inflate(Resource.Layout.FragmentSettings, container, false);

    //Grab the butten from the inflated fragment
    mBtnOk = view.FindViewById<Button>(Resource.Id.mBtnOk);
    mBtnOk.Click += (object sender, EventArgs e) =>
    {
        //DO stuff
    };

    //Start background task
    bworker = new BackgroundWorker();
    bworker.DoWork += Bworker_DoWork;
    bworker.RunWorkerCompleted += Bworker_RunWorkerCompleted;
    bworker.RunWorkerAsync();

    return view;
}


private void Bworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{   
   //Do stuff when the operation is completed
    bworker.RunWorkerAsync();
}

private void Bworker_DoWork(object sender, DoWorkEventArgs e)
{
    //Do the work in the background
}

EDIT: To make it run continuously, you can start the backgroundworker agin in the Bworker_RunWorkerCompleted method. (see the method)

I just dont think it is a good way implement a continuous thread, since i got the feeling it will take up alot of memory. You can try though.



来源:https://stackoverflow.com/questions/47306649/how-can-i-run-something-async-in-oncreateview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!