活动最小化然后恢复后视图再次可见

我的应用程序中出现了这个奇怪的事情。

我有一个AppCompatActivityViewPager里面有两个片段。我已添加LottieAnimationView到这两个片段中。

FragmentA我使用从服务器获取数据Retrofit时,它隐藏了LottieAnimationView. 在FragmentB(仅用于检查问题所在)中,我只是用来Handler隐藏LottieAnimationView3 秒后的内容。

现在,每当我最小化应用程序,然后再次打开它时,我都会看到LottieAnimationViewsetVisibility(View.GONE)只在FragmentA. 在 中,当我最小化并再次打开应用程序(按预期工作)后,FragmentB我看不到视图。setVisibility(View.GONE)

这是我在 中看到的图像FragmentA

https://img2.mukewang.com/64cc5ba700013c6105270751.jpg

处于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();

    }


慕妹3242003
浏览 116回答 2
2回答

手掌心

问题是,当您从背景中获取时, 会onResume被调用,因此您需要添加行来隐藏视图onResume,而不是onCreateView。

波斯汪

从 onCrete 中删除这一行并将其添加到 fetchDataFromServer() 方法中,如下所示。&nbsp; &nbsp; public class FragmentA extends Fragment {&nbsp; &nbsp; private AdapterEventStore mAdapter;&nbsp; &nbsp; private List<Item> mStore;&nbsp; &nbsp; private final String hostName = "https://xxx.xxx.xxx";&nbsp; &nbsp; private View view;&nbsp; &nbsp; private Context context;&nbsp; &nbsp; private LottieAnimationView lottieAnimationView;&nbsp; &nbsp; @Nullable&nbsp; &nbsp; @Override&nbsp; &nbsp; public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; view = inflater.inflate(R.layout.fragment_a, container, false);&nbsp; &nbsp; &nbsp; &nbsp; context = view.getContext();&nbsp; &nbsp; &nbsp; &nbsp; lottieAnimationView = view.findViewById(R.id.ais_lav_loading);&nbsp; &nbsp; &nbsp; &nbsp; lottieAnimationView.setVisibility(View.VISIBLE);&nbsp; &nbsp; &nbsp; &nbsp; initRecyclerView();&nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; }&nbsp; &nbsp; private void initRecyclerView() {&nbsp; &nbsp; &nbsp; &nbsp; RecyclerView store = view.findViewById(R.id.ais_rv_event_store);&nbsp; &nbsp; &nbsp; &nbsp; store.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));&nbsp; &nbsp; &nbsp; &nbsp; mStore = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; mAdapter = new AdapterEventStore(context, mStore);&nbsp; &nbsp; &nbsp; &nbsp; store.setAdapter(mAdapter);&nbsp; &nbsp; &nbsp; &nbsp; fetchDataFromServer();&nbsp; &nbsp; }&nbsp; &nbsp; private void fetchDataFromServer() {&nbsp; &nbsp; &nbsp; &nbsp; HttpLoggingInterceptor logging = new HttpLoggingInterceptor();&nbsp; &nbsp; &nbsp; &nbsp; logging.setLevel(HttpLoggingInterceptor.Level.BODY);&nbsp; &nbsp; &nbsp; &nbsp; OkHttpClient.Builder httpClient = new OkHttpClient.Builder();&nbsp; &nbsp; &nbsp; &nbsp; httpClient.addInterceptor(logging);&nbsp; &nbsp; &nbsp; &nbsp; final Retrofit retrofit = new Retrofit.Builder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .client(httpClient.build())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addConverterFactory(GsonConverterFactory.create())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .baseUrl(hostName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .build();&nbsp; &nbsp; &nbsp; &nbsp; APIGetItem itemShop = retrofit.create(APIGetItem.class);&nbsp; &nbsp; &nbsp; &nbsp; Call<ModelEventExclusive> call = itemShop.getEventStore(hostName);&nbsp; &nbsp; &nbsp; &nbsp; call.enqueue(new Callback<ModelEventExclusive>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onResponse(Call<ModelEventExclusive> call, Response<ModelEventExclusive> response) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Response: ", response.body().getItems().get(0).getItemName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mEventStore.addAll(response.body().getItems());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mAdapter.notifyDataSetChanged();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideLottieAnimation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFailure(Call<ModelEventExclusive> call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideLottieAnimation();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Error:"+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.e("Error occurred: ", t.getMessage());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; &nbsp; private void hideLottieAnimation(){&nbsp; &nbsp; &nbsp; &nbsp; lottieAnimationView.cancelAnimation();&nbsp; &nbsp; &nbsp; &nbsp; lottieAnimationView.setVisibility(View.GONE);&nbsp; &nbsp; }&nbsp; @Override&nbsp; &nbsp; public void onResume() {&nbsp; &nbsp; &nbsp; &nbsp; super.onResume();&nbsp; &nbsp; &nbsp; &nbsp; lottieAnimationView.setVisibility(View.GONE);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java