アクティビティが最小化して、復元された後、再び目に見える見ます

Vedprakash Wagh:

だから、私は私のアプリでは、この奇妙なことになってきました。

このことを考えてみましょう、私が持っているAppCompatActivity持っている、ViewPager二つの断片を含む、その中。私が追加したLottieAnimationViewフラグメントの両方の内側。

ではFragmentA私が使用してサーバーからデータをフェッチしていRetrofitて、負荷に、それが非表示になりますLottieAnimationViewFragmentB(のみ間違っているものをチェックするためのものである)、私は使用していますHandler非表示にするにはLottieAnimationView3秒後。

今、私はアプリを最小化し、再度それを開くたびに、私は見LottieAnimationViewIことsetVisibility(View.GONE)でのみFragmentAFragmentBたとき、私は、私はビューが表示されないsetVisibility(View.GONE)(期待どおりに動作する)、再びアプリを最小化し、開いた後。

ここで私が見るもののイメージですFragmentA

ここでは、画像の説明を入力します。

LottieAnimationView一時停止状態になっています。

ここに私のコードですFragmentA

public class FragmentA extends Fragment {

    private AdapterEventStore mAdapter;
    private List<Item> mStore;
    private final String hostName = "https://xxx.xxx.xxx";
    private View view;
    private Context context;
    private LottieAnimationView lottieAnimationView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_a, container, false);
        context = view.getContext();
        lottieAnimationView = view.findViewById(R.id.ais_lav_loading);
        lottieAnimationView.setVisibility(View.VISIBLE);
        initRecyclerView();
        return view;
    }


    private void initRecyclerView() {
        RecyclerView store = view.findViewById(R.id.ais_rv_event_store);
        store.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        mStore = new ArrayList<>();
        mAdapter = new AdapterEventStore(context, mStore);
        store.setAdapter(mAdapter);
        fetchDataFromServer();
    }

    private void fetchDataFromServer() {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);

        final Retrofit retrofit = new Retrofit.Builder()
                .client(httpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(hostName)
                .build();
        APIGetItem itemShop = retrofit.create(APIGetItem.class);

        Call<ModelEventExclusive> call = itemShop.getEventStore(hostName);
        call.enqueue(new Callback<ModelEventExclusive>() {
            @Override
            public void onResponse(Call<ModelEventExclusive> call, Response<ModelEventExclusive> response) {
                Log.e("Response: ", response.body().getItems().get(0).getItemName());
                mEventStore.addAll(response.body().getItems());
                mAdapter.notifyDataSetChanged();
                hideLottieAnimation();
            }

            @Override
            public void onFailure(Call<ModelEventExclusive> call, Throwable t) {
                hideLottieAnimation();
                Toast.makeText(context, "Error:"+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
                Log.e("Error occurred: ", t.getMessage());
            }
        });
    }

    private void hideLottieAnimation(){
        lottieAnimationView.cancelAnimation();
        lottieAnimationView.setVisibility(View.GONE);
    }
}

レイアウトのための FragmentA

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ais_rv_event_store"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/ais_lav_loading"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        app:lottie_autoPlay="true"
        app:lottie_loop="true"
        android:visibility="gone"
        app:lottie_repeatMode="reverse"
        app:lottie_rawRes="@raw/loading" />
</FrameLayout>

可視性があることに注意してくださいLottieAnimationViewに設定されているGONE最初に。アイテム100%の作品をフェッチする上記のコード私は実際の変数/クラス名を隠そうに悪い仕事をしてきたように、構文/変数名に誤りを指摘しないでください。

ここでは、コードにです FragmentB

public class FragmentB extends Fragment {
    private LottieAnimationView anim_loading;
    private View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_b, container, false);
        anim_loading = view.findViewById(R.id.lav_loading);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                anim_loading.cancelAnimation();
                anim_loading.setVisibility(View.GONE);
            }
        }, 3000);
        return view;
    }
}

あなたは私がここで間違ってやって教えていただけますか?なぜそれが一つの断片で期待どおりに動作し、他にはないのですか?それは私がレトロフィットコールバックの内側にそれを隠していますどのようにして何もする必要はありますか?

サイードエルAbady:

フォームの背景を取得するときに問題があるonResumeあなたがでビューを非表示にする行を追加する必要があるので、呼び出されますonResume、ではありませんonCreateView

チェックこの詳細についてはアウト

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=333433&siteId=1