1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.fragment.app.test;
18 
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.fragment.app.Fragment;
29 import androidx.fragment.test.R;
30 import androidx.loader.app.LoaderManager;
31 import androidx.loader.content.AsyncTaskLoader;
32 import androidx.loader.content.Loader;
33 import androidx.testutils.RecreatedActivity;
34 
35 public class LoaderActivity extends RecreatedActivity
36         implements LoaderManager.LoaderCallbacks<String> {
37     private static final int TEXT_LOADER_ID = 14;
38 
39     public TextView textView;
40     public TextView textViewB;
41 
42     @Override
onCreate(@ullable Bundle savedInstanceState)43     protected void onCreate(@Nullable Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45 
46         setContentView(R.layout.activity_loader);
47         textView = findViewById(R.id.textA);
48         textViewB = findViewById(R.id.textB);
49 
50         if (savedInstanceState == null) {
51             getSupportFragmentManager()
52                     .beginTransaction()
53                     .add(R.id.fragmentContainer, new TextLoaderFragment())
54                     .commit();
55         }
56     }
57 
58     @Override
onResume()59     protected void onResume() {
60         super.onResume();
61         LoaderManager.getInstance(this).initLoader(TEXT_LOADER_ID, null, this);
62     }
63 
64     @NonNull
65     @Override
onCreateLoader(int id, @Nullable Bundle args)66     public Loader<String> onCreateLoader(int id, @Nullable Bundle args) {
67         return new TextLoader(this);
68     }
69 
70     @Override
onLoadFinished(@onNull Loader<String> loader, String data)71     public void onLoadFinished(@NonNull Loader<String> loader, String data) {
72         textView.setText(data);
73     }
74 
75     @Override
onLoaderReset(@onNull Loader<String> loader)76     public void onLoaderReset(@NonNull Loader<String> loader) {
77     }
78 
79     static class TextLoader extends AsyncTaskLoader<String> {
TextLoader(Context context)80         TextLoader(Context context) {
81             super(context);
82         }
83 
84         @Override
onStartLoading()85         protected void onStartLoading() {
86             forceLoad();
87         }
88 
89         @Override
loadInBackground()90         public String loadInBackground() {
91             return "Loaded!";
92         }
93     }
94 
95     public static class TextLoaderFragment extends Fragment
96             implements LoaderManager.LoaderCallbacks<String> {
97         public TextView textView;
98 
99         @Override
onCreate(@ullable Bundle savedInstanceState)100         public void onCreate(@Nullable Bundle savedInstanceState) {
101             super.onCreate(savedInstanceState);
102             LoaderManager.getInstance(this).initLoader(TEXT_LOADER_ID, null, this);
103         }
104 
105         @Nullable
106         @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)107         public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
108                 @Nullable Bundle savedInstanceState) {
109             return inflater.inflate(R.layout.fragment_c, container, false);
110         }
111 
112         @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)113         public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
114             textView = view.findViewById(R.id.textC);
115         }
116 
117         @NonNull
118         @Override
onCreateLoader(int id, @Nullable Bundle args)119         public Loader<String> onCreateLoader(int id, @Nullable Bundle args) {
120             return new TextLoader(getContext());
121         }
122 
123         @Override
onLoadFinished(@onNull Loader<String> loader, String data)124         public void onLoadFinished(@NonNull Loader<String> loader, String data) {
125             textView.setText(data);
126         }
127 
128         @Override
onLoaderReset(@onNull Loader<String> loader)129         public void onLoaderReset(@NonNull Loader<String> loader) {
130         }
131     }
132 }
133