I am reading about how to interact between UI and background thread here.
This article has following note:
The AsyncTask does not handle configuration changes automatically,
i.e. if the activity is recreated. The programmer has to handle that
in his coding. A common solution to this is to declare the AsyncTask
in a retained headless fragment.
I dont understand what is retained headless fragment.
For example, in this way I can add fragment:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame, new MyFragment());
transaction.commit();
And in fragment I can execute AsyncTask like this:
MyTask myTask = new MyTask();
String[] args = {"Hello"};
myTask.execute(args);
Is this called "to declare the AsyncTask in a retained headless fragment"?
Headless fragment is nothing but a fragment which does not have a view. In onCreate() of the fragment lifeCycle, use setRetainInstance(true);. This will not destroy the fragment even if the activity recreates. So if an AsyncTask is running in fragment, on recreation of the activity, you wont lose the AsyncTask.
In onCreate of the activity, you have to add the fragment with a tag. Before adding, check if the fragment exist using getFragmentManager().findFragmentByTag(TAG), if the fragment is null then create a new instance of the fragment and add it.
In Fragment there will not be any view inflated, so no need to override onCreateView().
An example of headlessFragment :
public class HeadlessProgressFragment extends Fragment {
private ProgressListener mProgressListener;
private AsyncTask<Void, Integer, Void> mProgressTask;
public interface ProgressListener {
void updateProgress(int progress);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public void setProgressListener(Context context) {
mProgressListener = (ProgressListener) context;
}
public void startProgress(final int size) {
if (mProgressTask == null || mProgressTask.getStatus() != AsyncTask.Status.RUNNING || mProgressTask.getStatus() == AsyncTask.Status.FINISHED) {
mProgressTask = new AsyncTask<Void, Integer, Void>() {
#Override
protected Void doInBackground(Void... params) {
for (int index = 0; index < size; index++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
publishProgress(index + 1);
}
}
return null;
}
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (mProgressListener != null) {
mProgressListener.updateProgress(values[0]);
}
}
};
mProgressTask.execute();
}
}
}
In Activity Something like this :
public class MainActivity extends FragmentActivity implements HeadlessProgressFragment.ProgressListener {
private static final String TAG = "progress_fragment";
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy_view);
mHeadlessProgressFragment = (HeadlessProgressFragment) getSupportFragmentManager().findFragmentByTag(TAG);
if (mHeadlessProgressFragment == null) {
mHeadlessProgressFragment = new HeadlessProgressFragment();
getSupportFragmentManager().beginTransaction().add(mHeadlessProgressFragment,TAG).commit();
}
mHeadlessProgressFragment.setProgressListener(this);
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
final Button startFillBtn = (Button) findViewById(R.id.btn_start_filling);
startFillBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mHeadlessProgressFragment.startProgress(100);
}
});
}
#Override
public void updateProgress(int progress) {
mProgressBar.setProgress(progress);
}
}
As i simplified the complexity in my case by Just update your UI (if you have to) by checking the calling fragment or activity is present or not. Start the asynctask by assigning the weakreference of calling entity.
Related
I have two classes corresponding to two activities in my code.
What I would like, is that the functions in the class of the activity B is launched in the activity A, but without showing the activity : I would just like the code to be executed...
How can I launch the functions of the activity B in background from the activity A?
I read to use Services, but I don't know at all how to use them, I don't see so many reading about it. I don't know if it is the good way.
I tried this with no success: startService(new Intent(this, HereMap.class));
Activity A:
public class LoginActivity extends AppCompatActivity {
#Override
protected void onResume(){
...
//Code to execute the activity B in background
Intent activityB= new Intent(this, HereMap.class);
activityB.onDownloadButtonClicked(); //=> here is the execution but doesn't work...
}
#Override
protected void onCreate(Bundle savedInstanceState) {
...
}
Activity B:
public class HereMap extends AppCompatActivity {
...
private MapLoader.Listener mapLoaderHandler = new MapLoader.Listener() {
#Override
public void onProgress(int progress) {
Log.i(TAG, "Progress " + progress + "%");
downloadProgressBar.setProgress(progress);
}
#Override
public void onInstallationSize(long diskSize, long networkSize) {
Log.i(TAG, "Map data require " + diskSize);
}
#Override
public void onGetMapPackagesComplete(MapPackage rootMapPackage,
MapLoader.ResultCode resultCode) {
if (resultCode == MapLoader.ResultCode.OPERATION_SUCCESSFUL) {
Log.i(TAG, "Map packages received successful: " + rootMapPackage.getTitle());
currentInstalledMaps = new ArrayList<>(1);
populateInstalledMaps(rootMapPackage);
} else {
Log.e(TAG, "Can't retrieve map packages: " + resultCode.name());
Toast.makeText(HereMap.this,
"Error: " + resultCode.name(), Toast.LENGTH_SHORT).show();
return;
}
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Init map
setContentView(R.layout.activity_here_map);
mapFragment = (AndroidXMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment);
...
public void onDownloadButtonClicked() {
Log.d(TAG, "Downloading new map data...");
List<Integer> downloadList = new ArrayList<>(1);
downloadList.add(120214); //Id:120002 Berlin Id:120214, Name: Andalucia, Size:231504 Cherche l'id avec l'application iOS map-downloader-ios-swift
downloadProgressBar.setProgress(0);
downloadProgressBar.setVisibility(View.VISIBLE);
MapLoader.getInstance().installMapPackages(downloadList);
}
If you have some function you want to use in multiple Activities, you can put it in its own class and instantiate that class in whatever place you need to call its methods. The created class should not extend Activity or Fragment if it isn't actually an Activity or Fragment (this will cause all sorts of bugs). If it needs access to a Context or other Android component, you could pass those in at construction or to the methods that need it.
If you need your helper class to interact with its hosting activity (e.g. show or hide views, send data to views, etc) you can define interfaces on the helper or pass the views in directly.
If you define interfaces on your helper class you can use those to "call back" to whatever activity is hosting it (e.g. to show a progress bar). If you don't want to use an interface you could just pass the view to the helper class too, but the interface approach is sometimes more flexible.
Here are examples of both approaches:
Using an Interface
public class MapHelper {
public interface ProgressBarHolder {
void showBar();
}
private ProgressBarHolder progress;
MapHelper(ProgressBarHolder prog) {
progress = prog;
}
public void onDownloadButtonClicked() {
List<Integer> downloadList = new ArrayList<>(1);
downloadList.add(120214);
progress.showBar();
MapLoader.getInstance().installMapPackages(downloadList);
}
}
then you can call it from any Activity, like this
public class MyActivity extends AppCompatActivity
implements MapHelper.ProgressBarHolder
{
private MapHelper helper = new MapHelper(this);
private ProgressBar bar;
#Override
public void showBar() {
// show a progress bar, MapHelper will call this
// when it needs the current activity to show a progress bar
bar.setProgress(0);
bar.setVisibility(View.VISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set up stuff
bar = findViewById(R.id.progress);
bar.setVisibility(View.GONE);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
helper.onDownloadButtonClicked();
}
});
}
}
Passing in Views
public class MapHelper {
private ProgressBar progress;
MapHelper(ProgressBar prog) {
progress = prog;
}
public void onDownloadButtonClicked() {
List<Integer> downloadList = new ArrayList<>(1);
downloadList.add(120214);
progress.setProgress(0);
progress.setVisibility(View.VISIBLE);
MapLoader.getInstance().installMapPackages(downloadList);
}
}
then you can call it from any Activity, like this
public class MyActivity extends AppCompatActivity {
private MapHelper helper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set up stuff
ProgressBar bar = findViewByid(R.id.progress);
bar.setVisibility(View.GONE);
// Wait to create the helper until you have the views
helper = new MapHelper(bar);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
helper.onDownloadButtonClicked();
}
});
}
}
I am using Pager Adapter and I want to call fragment method from activity. I tried with callback interface but I get null pointer exception because Fragment fragment = new Fragment() doesn't call onCreate() of that fragment. Any ideas how should I do this? This is my code:
MainActivity:
public interface Communicator {
void passStatus(String status);
}
private Communicator communicator;
public void setCommunicator(Communicator communicator)
{
this.communicator = communicator;
}
#Override
protected void onPause() {
super.onPause();
if(communicator != null)
{
communicator.passStatus("STOP");
}
}
#Override
protected void onResume() {
super.onResume();
if(communicator != null)
{
communicator.passStatus("START");
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabLayout = findViewById(R.id.tabBar);
viewPager = findViewById(R.id.viewPager);
Fragment fragment = new Fragment();
setCommunicator(new Communicator() {
#Override
public void passStatus(String status) {
if(status == "START")
{
fragment.Start();
}
else if(status == "STOP")
{
fragment.Stop();
}
}
});
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pagerAdapter.addFragment(fragment, "Some fragment");
pagerAdapter.addFragment(new AnotherFragment(), "Another fragment");
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
You can use SharedViewModel : https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
You will create SharedViewModel in activity and export a LiveData for start or stop.
In your fragment, you will observer LiveData of SharedViewModel.
Class for one call event:
import androidx.lifecycle.Observer
open class Event<out T>(private val content: T) {
#Suppress("MemberVisibilityCanBePrivate")
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
class EventObserver<T>(private val onEventUnhandledContent: (T) -> Unit) : Observer<Event<T>> {
override fun onChanged(event: Event<T>?) {
event?.getContentIfNotHandled()?.let {
onEventUnhandledContent(it)
}
}
}
You can this, either by using SharedViewModel, Interfaces callbacks, Broadcast Receivers and also by using getSupportFragmentManager.
But the thing is that you will have to manage the things with null checks while accessing the views of the Fragment because in case if the Fragment is detached that will give a NullPointerException.
I have an app consisting of (among other things) one Activity and one Fragment. In the Activity som data is displayed using TextViews, and in the Fragment the user can input some data using EditText-views. I have a method which takes data from both the Activity and Fragment, performs some calculations and finally displays a result in both the Fragment and the Activity. This method currently works when I call if after editing the text in any of the EditText-views in the Fragment. However, I also want to call it each time I update the data in the Activity but when I attempt this, I can not fetch the EditText data because they return null.
So my question is: what is good practice, or the ”right way” to do when dealing with methods that are supposed to be reached from both an Activity and a Fragment? I would greatly appreciate if someone could lead me onto the right track.
I have read the official documentation on Fragments and there ViewModel was mentioned. But this doesn’t seem suitable in for my application since I want the Activity to be involved. Do I need to use this or can I go through my main Activity? I’ve also read about Interfaces, but I’m not sure which one would suit my project best. I’m currently using Interfaces, but I’m not sure if I’m doing it correctly.
I’ve also watched this video and read these following questions:
Shared ViewModel to help communication between fragments and parent activity
Call a fragmentMethod from another fragment, but can't refer to them in parentFragmentActivity's View pager
how to manage the fragments in android correctly?
"My God"’s reply to this question was helpful but I’m still not sure what is the best thing to do in my case, as I have a Fragment where user can input data, and the same fragment should also view data as a result of that input. (Maybe my first mistake is building the app like this?)
I provide some code in case it is helpful.
public class AccuracyFragment extends Fragment {
EditText editTextLevel, editTextAccuracy;
private OnFragmentInteractionListener mListener;
public AccuracyFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_accuracy, container, false);
editTextAccuracy = view.findViewById(R.id.text_accuracy_character);
editTextLevel = view.findViewById(R.id.text_level_character);
TextWatcher watcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//Doing nothing
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//Doing nothing
}
#Override
public void afterTextChanged(Editable editable) {
updateFragment(Float.valueOf(editTextLevel.getText().toString()), Float.valueOf(editTextAccuracy.getText().toString()));
}
};
editTextLevel.addTextChangedListener(watcher);
editTextAccuracy.addTextChangedListener(watcher);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null; //I don’t know what this does
}
#Override
public void onResume() {
super.onResume();
// updateFragment(); //Should I have this?
}
public interface OnFragmentInteractionListener {
String[] onAccuracyFragmentInputChanged(float levelFromFragment, float accuracyFromFragment); }
public void updateFragment(float level, float accuracy) {
//Complicated method doing things with editTextLevel and editTextAccuracy. However, it doesn’t work when this method is called from outside AccuracyFragment – EditTexts are null
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onPause() {
super.onPause();
}
#Override
public void onDestroyView() {
super.onDestroyView();
}
#Override
public void onStop() {
super.onStop();
}
}
public class MainActivity extends AppCompatActivity implements AccuracyFragment.OnFragmentInteractionListener, AdapterView.OnItemSelectedListener {
AccuracyFragment accuracyFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_new);
selectedID = null;
textView1 = (TextView) findViewById(R.id.text_1);
textView2 = (TextView) findViewById(R.id.text_2);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state, then we don't need to do anything and should return or else we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
accuracyFragment = new AccuracyFragment();
}
}
//This is the core method which takes the value from two EditTexts in the Fragment, and two TextViews in the MainActivity
private String[] getRequiredAccuracy(float firstValueFromActivity, float secondValueFromActivity, float firstValueFromFragment, float secondValueFromFragment) {
//This methods uses parameters from the Activity, and two from the Fragment, and is intended to be called from both the Activity and from the Fragment itself
String returnValues[] = {s, q, r, c}; //This method is too complex to show, but it will end up outputting some values
return returnValues;
}
public void methodCalledUponClick(View view) {
//After showing a Dialog with some choices, I intend to call the method from fragment:
accuracyFragment.updateFragment();
}
#Override
public String[] onAccuracyFragmentInputChanged(float levelFromFragment, float accuracyFromFragment) {
String returnValues[] = {"0", "0", "0"};
if (selectedID != null) {
if (textView1.length() == 0 || textView2.length() == 0) {
//Do nothing if any of these are empty
} else {
returnValues = getRequiredAccuracy(Float.valueOf(textView1.getText().toString()), Float.valueOf(textView2.getText().toString()), levelFromFragment, accuracyFromFragment);
}
}
return returnValues;
}
}
}
You can go ahead with callback/Interface to communicate with fragment and activity simultaneously.
For Creating callback/Interface:
public interface CallBackListener {
void onCallBack(String value);// pass any parameter in your onCallBack which you want to return
}
In Fragment Class:
public class AccuracyFragment extends Fragment {
private CallBackListener callBackListener;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//getActivity() is fully created in onActivityCreated and instanceOf differentiate it between different Activities
if (getActivity() instanceof CallBackListener)
callBackListener = (CallBackListener) getActivity();
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
EditText editText = (EditText) view.findViewById(R.id.edittext);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(callBackListener != null)
callBackListener.onCallBack(s.toString());
}
});
}
}
In your Activity:
public class MainActivity extends AppCompatActivity implements CallBackListener
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onCallBack(String value) {
Toast.makeText(mContext,"onCallback Called",Toast.LENGTH_LONG).show();
}
}
I'm new to android and I'm trying to get a hang of creating and using Fragments.
I have a fragment that shows a simple list of multiple dates to choose from and implements an onClickListener. The idea is once a user chooses a date, the fragments sends the date back to the MainActivity which then runs a query in database and sends the database response to another fragment.
I'm stuck on the point of sending the date back to MainActivity, elegantly. I can't find much info. I found this:
Activity activity = getActivity();
if(activity instanceof MyActivity){
MyActivity myactivity = (MyActivity) activity;
myactivity.myMethod();
}
I'm very new to this but this seems hacky to me. Is this the right way or is there another way?
Any input is appreciated
I prefer the interface based approach because is very clean. You can declare a nested interface in your Fragment or an external one:
interface OnMyStuffListener {
void myMethod();
}
Make the Activity to implement that interface:
public class MainActivity extends Activity implements OnMyStuffListener {
#Override
public void myMethod() {
// Do whatever you want.
}
}
The Fragment will be attached to the Activity so you can check the instance of the Context and cast it to the Activity:
public class MyFragment extends Fragment implements View.OnClickListener {
private OnMyStuffListener mListener;
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMyStuffListener) {
mListener = (OnMyStuffListener) context;
} else {
throw new IllegalArgumentException("The context " + context.getClass().getName() +
"must implement " + OnMyStuffListener.class.getName());
}
}
#Override
public void onDetach() {
super.onDetach();
// Release it avoiding memory leak.
mListener = null;
}
#Override
public void onClick(View v) {
mListener.myMethod();
}
}
YES this is absolutely right. You can use this, if you are not sure that your Fragment is attached to Activity
You can also achieve this by using Interface, using an EventBus like LocalBroadcastManager, or starting a new Activity with an Intent and some form of flag passed into its extras Bundle or something else.
Here is an example about using Interface:
1. Add function sendDataToActivity() into the interface (EventListener).
//EventListener.java
public interface EventListener {
public void sendDataToActivity(String data);
}
2. Implement this functions in your MainActivity.
// MainActivity.java
public class MainActivity extends AppCompatActivity implements EventListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void sendDataToActivity(String data) {
Log.i("MainActivity", "sendDataToActivity: " + data);
}
}
3. Create the listener in MyFragment and attach it to the Activity.
4. Finally, call function using listener.sendDataToActivity("Hello World!").
// MyFragment.java
public class MyFragment extends Fragment {
private EventListener listener;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if(activity instanceof EventListener) {
listener = (EventListener)activity;
} else {
// Throw an error!
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
// Send data
listener.sendDataToActivity("Hello World!");
return view;
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Hope this will help~
I know I came a bit too late to the party and over the past 3 years (or maybe longer) there've been thousands and thousands of discussion like this one (unfortunately most of them involving activities and not fragments), but I'm curious about one thing.
Having the following piece of code, can mFragment be null when the AsyncTask's callback onError() or onSuccess() gets called after the screen orientation changes? So the AsyncTask starts, I rotate the screen of the device and at some point the AsyncTask will return an error or a success. When this happens, can the mFragment be null?
I already know that this cannot happen when dealing with Activities because basically no messages will be processed between a call to onRetainNonConfigurationInstance() of the previous instance and onCreate() of the new instance of the activity.
Unfortunately I couldn't find anything about this rule when it comes to fragments.
Do you guys know anything about this?
Thank you for your answers!
public class Fragment extends Fragment {
private WorkerFragment mWorkerFragment;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWorkerFragment =(WorkerFragment)getFragmentManager().findFragmentByTag("worker_fragment");
if (mWorkerFragment == null) {
mWorkerFragment = new WorkerFragment();
getFragmentManager().beginTransaction().add(mWorkerFragment, "worker_fragment").commit();
}
if (savedInstanceState == null) {
mWorkerFragment.performAsyncTask();
}
private static class WorkerFragment extends Fragment {
private TypeOfAsyncTask mAsyncTask;
private Fragment mFragment;
private Callback mCallback = new Callback() {
#Override
public void onError(Error error) {
// Can mFragment be null here?
mFragment.onSendRequestError(error);
}
#Override
public void onSuccess(Result result) {
// Can mFragment be null here?
mFragment.onSendRequestSuccess(result);
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mFragment = (Fragment) getTargetFragment();
}
#Override
public void onDetach() {
super.onDetach();
mFragment = null;
}
private void performAsyncTask() {
mAsyncTask = new TypeOfAsyncTask(mCallback);
mAsyncTask.execute();
}
}
}