activity 最小化后再恢复后视图再次可见
View visible again after activity is minimized and then restored
我的应用程序中出现了这个奇怪的东西。
我有一个AppCompatActivity
,里面有ViewPager
,里面有两个片段。我在两个片段中都添加了 LottieAnimationView
。
在 FragmentA
中,我使用 Retrofit
从服务器获取数据,在加载时,它隐藏了 LottieAnimationView
。在 FragmentB
中(仅用于检查错误),我只是使用 Handler
在 3 秒后隐藏 LottieAnimationView
。
现在,每当我最小化应用程序,然后再次打开它时,我只在 FragmentA
中看到 setVisibility(View.GONE)
的 LottieAnimationView
。在 FragmentB
中,当我 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% 有效,请不要指出 syntax/variable 名称中的任何错误,因为我在试图隐藏实际 variable/class 名称方面做得不好。
这是 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;
}
}
你能告诉我这里做错了什么吗?为什么它在一个片段中按预期工作而在另一个片段中却没有?它与我将其隐藏在 Retrofit 回调中的方式有什么关系吗?
从 conCrete 中删除这一行并将其添加到像这样的从 Server() 方法获取数据中。
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);
}
@Override
public void onResume() {
super.onResume();
lottieAnimationView.setVisibility(View.GONE);
}
}
问题是,当您从后台获取时,会调用 onResume
,因此您需要添加行以隐藏 onResume
中的视图,而不是 onCreateView
.
查看 Android Developer page on fragments 了解更多详情。
我的应用程序中出现了这个奇怪的东西。
我有一个AppCompatActivity
,里面有ViewPager
,里面有两个片段。我在两个片段中都添加了 LottieAnimationView
。
在 FragmentA
中,我使用 Retrofit
从服务器获取数据,在加载时,它隐藏了 LottieAnimationView
。在 FragmentB
中(仅用于检查错误),我只是使用 Handler
在 3 秒后隐藏 LottieAnimationView
。
现在,每当我最小化应用程序,然后再次打开它时,我只在 FragmentA
中看到 setVisibility(View.GONE)
的 LottieAnimationView
。在 FragmentB
中,当我 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% 有效,请不要指出 syntax/variable 名称中的任何错误,因为我在试图隐藏实际 variable/class 名称方面做得不好。
这是 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;
}
}
你能告诉我这里做错了什么吗?为什么它在一个片段中按预期工作而在另一个片段中却没有?它与我将其隐藏在 Retrofit 回调中的方式有什么关系吗?
从 conCrete 中删除这一行并将其添加到像这样的从 Server() 方法获取数据中。
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);
}
@Override
public void onResume() {
super.onResume();
lottieAnimationView.setVisibility(View.GONE);
}
}
问题是,当您从后台获取时,会调用 onResume
,因此您需要添加行以隐藏 onResume
中的视图,而不是 onCreateView
.
查看 Android Developer page on fragments 了解更多详情。