Search code examples
androidandroid-fragmentsandroid-activityokhttp

How to modify fragment`s view form other thread?


I have an a fragment and okhttpp client. When i make a request inside fragment, and okhttp get 200 response id like to set mProgressBar.setVisibility(View.GONE); inside my fragment. But unfortunately nothing happens. I get an reponse, and its ok , code goes to line where mProgressBar.setVisibility(View.GONE); but nothing happens, mProgressBar still showing, but logs show mProgressBar not visible. Why so?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_shemas, container, false);
    ls = (Ls) getActivity().getApplicationContext();
    mProgressBar = rootView.findViewById(R.id.pb_fr_schema);

    mClient = ls.createClient(getActivity());
    Log.d(TAG, "Назначили OkHttpClient " + mClient.toString() + " для " + this.getClass().toString() + " в " + this.getClass().toString());

    try
    {
        String url = ls.baseUrl + ls.getLsId() + "/" + ls.getCurrBidId() + "/call-fwd-settings/overview";
        requestOkHttp(url, "GET", "");
    } catch (IOException e)
    {
        e.printStackTrace();
    }

    mSpinner = (Spinner) rootView.findViewById(R.id.sp_fr_schemas_did);
    // Список номеров
    ArrayList<String> didsNames = new ArrayList<>();
    for (Map.Entry<String, Bid> pr : ls.getProducts().entrySet())
    {
        didsNames.add(pr.getValue().getBidName());
    }

    mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_row_products, R.id.tv_bid_name, didsNames);
    // Чтобы был читаемый дизайн при выборе DID
    mAdapter.setDropDownViewResource(R.layout.spinner_row_products);
    //spinner.setAdapter(mAdapter);
    return inflater.inflate(R.layout.fragment_shemas, container, false);
}

Here is okhttp response code

getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
// Отключаем mProgressBar
mProgressBar.setVisibility(View.GONE);
if (mProgressBar.getVisibility() == View.VISIBLE) {
Log.d(TAG, " mProgressBar visible");
} else {
Log.d(TAG, " mProgressBar not visible");
}
}
});

Solution

  • I found a solution. Problem was in return. I change this return inflater.inflate(R.layout.fragment_shemas, container, false); to this return rootView; and now its ok.