How can I inject params from bundles at runtime with dagger.android? - android

I'm working on wrapping my head around Dagger 2 and I've came upon a situation that I'm not really sure how to properly solve. I'm using dagger.android to set up subcomponents for each of my activities and fragments, where each subcomponent has a presenter with it's own dependencies.
The problem is that one fragment should display info fetched from the web, so I'm passing in a URL to the fragment to tell it what to display. But I'm not sure how to get Dagger too resolve the dependency on fragment load. I thought I needed a subcomponent builder, but that's apparently redundant as the compiler message is telling me it need the URL from a Provides method. I've also set up a qualifier for the URL and annotated a method in the fragment with it. I thought Dagger might pick it up, but I was wrong. Can someone give me an idea as to how best handle this situation?

If I am not wrong you are trying to provide url dependency in Fragment which is provided via Bundle. In that case add Provides method in Module which will take Fragment as parameter. As Fragment instance is on the graph it will automatically satisfy.
Fragment:
#Inject
String url;
public static DetailFragment newInstance() {
Bundle args = new Bundle();
args.putString("url", "http://stackoverflow.com/");
DetailFragment fragment = new DetailFragment();
fragment.setArguments(args);
return fragment;
}
#Override
public void onAttach(Context context) {
AndroidSupportInjection.inject(this);
super.onAttach(context);
printInfo();
}
public void printInfo(){
if (url == null) {
Log.i(TAG, "Url is null");
}else {
Log.i(TAG, "Url: " + url);
}
}
Module:
#Module
public class DetailFragmentModule {
#Provides
String provideUrl(DetailFragment detailFragment){
Bundle bundle = detailFragment.getArguments();
String url = bundle.getString("url");
return url;
}
#Provides
DetailFragmentView provideDetailFragmentView(DetailFragment detailFragment){
return detailFragment;
}
}
As you are injecting url in DetailFragment it will go to provideUrl method and satisfy DetailFragment and it will return url from Bundle.
This worked for me. :)

Related

Passing arguments from Fragment to VIewModel/reuse Fragment

I'm learning how to build apps using mvvm approach. In my app I have activity with two fragments and ViewPager. So what I'm trying to solve now is how to reuse fragment class, because the are basically identical, the only difference is the data that I want to observe from ViewModel depending on which fragment is displayed. So what I've done so far
stateAdapter.addFragment(BenchmarksFragment.newInstance("collections"), getString(R.string.collection));
stateAdapter.addFragment(BenchmarksFragment.newInstance("maps"), getString(R.string.map));
In activity I have adapter that creates fragments with arguments.
public static BenchmarksFragment newInstance(String value) {
final BenchmarksFragment fragment = new BenchmarksFragment();
final Bundle args = new Bundle();
args.putString(KEY, value);
fragment.setArguments(args);
return fragment;
}
Further in fragment onCreate I pass argument to VMFactory
String arg = getArguments() == null ? DEFAULT : getArguments().getString(KEY);
BenchmarksVMFactory factory = new BenchmarksVMFactory(requireActivity().getApplication(), arg);
In ViewModel constructor I simply assign this argument to variable
public BenchmarksViewModel(#NonNull Application application, String arg) {
super(application);
this.arg = arg;
Log.d("TAG", "BenchmarksViewModel: " + arg);
}
Through logs I see that iI get only argument from first fragment "collections".
So my question is how to correctly pass arguments to ViewModel and further manage the logic of displaying the right list(for "collections" fragment collectionsList and for "maps" fragment mapList) using same fragment class?
If you can advise me articles/examples to read I would appreciate that.
UPDATE
I was able to get argument from second fragment by changing
model = new ViewModelProvider(requireActivity(), factory).get(BenchmarksViewModel.class);
requireActivity() to getViewModelStore()
model = new ViewModelProvider(getViewModelStore(), factory).get(BenchmarksViewModel.class);

Passing Objects to fragment without argument

What will be the implications if I passed an object to a fragment without using bundle? I just opened one of the old code and found it there and couldn't help asking this question as I have never tried it. Also I am not finding any memory leaks in it.
This is how it is implemented -
Activity class:
MyFragment fragment =
MyFragment.newInstance(getIntent().getStringExtra(DATA),
instance.getCallback(),
instance.getRequest());
getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Fragment class:
public class MyFragment extends Fragment {
public MyFragment() {
/* Required empty public constructor */
}
public static MyFragment newInstance(String data, Callback callback,
Request request) {
MyFragment fragment = new MyFragment();
fragment.setCallback(callback);
fragment.setRequest(request);
fragment.setData(data);
return fragment;
}
private void setCallback(Callback callback) {
this.callback = callback;
}
private void setData(Data data) {
this.data = data;
}
private void setRequest(Request request) {
this.request = request;
}
}
Generally, what I have used till date is Fragment#setArguments(Bundle)
Please check out this answer: Why its not recommended to pass object to fragment with simple getter setter.
Long story short, you will lose your data on configuration changes.
There are mainly two ways of communicating with a fragment: via bundles or via an interface that you implement in your activity. Please see this link in order to see how to properly communicate with a fragment via an interface: https://developer.android.com/training/basics/fragments/communicating

Android / Dagger2 - How to add bundle arguments ? Inject fragment or use newInstance?

I'm looking to find a solution on how to inject fragment and pass arguments to it.
And i didn't find any proper solution because injecting the fragment means by the constructor which is not safe for states.
Is there any way to do this, without calling the newInstance pattern ?
Thanks,
Best.
Because Android manages the lifecycle of your Fragment, you should separate the problems of passing state into the Fragment through its bundle and injecting the Fragment with injectable deps. Usually, the best way to separate these is by providing a static factory method, which you might be calling the newInstance pattern.
public class YourFragment extends Fragment {
// Fragments must have public no-arg constructors that Android can call.
// Ideally, do not override the default Fragment constructor, but if you do
// you should definitely not take constructor parameters.
#Inject FieldOne fieldOne;
#Inject FieldTwo fieldTwo;
public static YourFragment newInstance(String arg1, int arg2) {
YourFragment yourFragment = new YourFragment();
Bundle bundle = new Bundle();
bundle.putString("arg1", arg1);
bundle.putInt("arg2", arg2);
yourFragment.setArguments(bundle);
return yourFragment;
}
#Override public void onAttach(Context context) {
// Inject here, now that the Fragment has an Activity.
// This happens automatically if you subclass DaggerFragment.
AndroidSupportInjection.inject(this);
}
#Override public void onCreate(Bundle bundle) {
// Now you can unpack the arguments/state from the Bundle and use them.
String arg1 = bundle.getString("arg1");
String arg2 = bundle.getInt("arg2");
// ...
}
}
Note that this is a different type of injection than you may be used to: Rather than getting a Fragment instance by injecting it, you are telling the Fragment to inject itself later once it has been attached to an Activity. This example uses dagger.android for that injection, which uses subcomponents and members-injection methods to inject #Inject-annotated fields and methods even when Android creates the Fragment instance outside of Dagger's control.
Also note that Bundle is a general key-value store; I've used "arg1" and "arg2" instead of coming up with more creative names, but you can use any String keys you'd like. See Bundle and its superclass BaseBundle to see all of the data types Bundle supports in its get and put methods. This Bundle is also useful for saving Fragment data; if your app is interrupted by a phone call and Android destroys your Activity to save memory, you can use onSaveInstanceState to put form field data into the Bundle and then restore that information in onCreate.
Finally, note that you don't need to create a static factory method like newInstance; you could also have your consumers create a new YourFragment() instance and pass in a particular Bundle design themselves. However, at that point the Bundle structure becomes a part of your API, which you may not want. By creating a static factory method (or Factory object or other structure), you allow the Bundle design to be an implementation detail of your Fragment, and provide a documented and well-kept structure for consumers to create new instances.

Could not find Fragment constructor

I am facing the issue on some devices and getting an error on my crash analytics. A lot of user devices are facing this issue, but on my device it's working fine.
Unable to start activity ComponentInfo{com.ox.outloks.new/com.ox.outloks.new.activities.MainDrawerActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.alchemative.outfitters.outfitters.fragments.ProductsFragment: could not find Fragment constructor
The error is coming at the line which is in activity super.onCreate(savedInstanceState);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Here is ProductsFragment constructor
#SuppressLint("ValidFragment")
public ProductsFragment(String id) {
categoryID = id;
}
All Fragment classes you create must have a public, no-arg constructor. In general, the best practice is to simply never define any constructors at all and rely on Java to generate the default constructor for you. But you could also write something like this:
public ProductsFragment() {
// doesn't do anything special
}
If your fragment needs extra information, like String id in your posted example, a common pattern is to define a newInstance() static "factory method" that will use the arguments Bundle to give that info to your fragment.
public static ProductsFragment newInstance(String id) {
Bundle args = new Bundle();
args.putString("id", id);
ProductsFragment f = new ProductsFragment();
f.setArguments(args);
return f;
}
Now, rather than calling new ProductsFragment(id), you'll call ProductsFragment.newInstance(id). And, inside your fragment, you can access the id by calling getArguments().getString("id").
By leveraging the arguments bundle (instead of creating a special constructor), your fragment will be able to be destroyed and recreated by the Android framework (e.g. if the user rotates their phone) and your necessary info (the id) will persist.
Accepted answer is not entirely correct as of today. With FragmentFactory
you can create fragment such as
MyFragment(arg1:Any, arg2:Any,...) {
}
and instantiate it inside FragmentFactory's instantiate method
override fun instantiate(classLoader: ClassLoader, className: String): Fragment {
return when (className) {
MyFragment::class.java.name -> MyFragment(arg1, arg2)
}
}
and set your FragmentFactory as supportFragmentManager's fragment factory before onCreate of Activity, because Android checks out for empty constructor fragment and if you don't provide before onCreate your app will crash as usual behavior.
supporFragmentManager.fragmentFactory = yourFragmentFactoryInstance
A newer alternative to communicate between an Activity and a Fragment, thus allowing that the Fragment can have an empty constructor would be the use of ViewModels.
I have just faced this exception. My mistake was calling requireArguments() method before arguments were received by the Fragment
The problem comes from below method of fragment class that basically shows how we should basically do initialisation of our fragment classes in general. First see that method below:-
public static Fragment instantiate(#NonNull Context context, #NonNull String fname,
#Nullable Bundle args) {
try {
Class<? extends Fragment> clazz = FragmentFactory.loadFragmentClass(
context.getClassLoader(), fname);
Fragment f = clazz.getConstructor().newInstance();
if (args != null) {
args.setClassLoader(f.getClass().getClassLoader());
f.setArguments(args);
}
return f;
} catch (java.lang.InstantiationException e) {
throw new InstantiationException("Unable to instantiate fragment " + fname
+ ": make sure class name exists, is public, and has an"
+ " empty constructor that is public", e);
} catch (IllegalAccessException e) {
throw new InstantiationException("Unable to instantiate fragment " + fname
+ ": make sure class name exists, is public, and has an"
+ " empty constructor that is public", e);
} catch (NoSuchMethodException e) {
throw new InstantiationException("Unable to instantiate fragment " + fname
+ ": could not find Fragment constructor", e);
} catch (InvocationTargetException e) {
throw new InstantiationException("Unable to instantiate fragment " + fname
+ ": calling Fragment constructor caused an exception", e);
}
}
So if you will see problem lies with catch (NoSuchMethodException e) code block which triggers in this case since its not able detect contructor of the fragment from line Fragment f = clazz.getConstructor().newInstance();. If you will see the function getConstructor(), You will observe this will reponsible to make this exception since it throws this NoSuchMethodException and same have caught inside Fragment instantiate funtion. Also if you move further on above function the recommended approach for sending params to a fragment arguments is also given. So now we are all clear about what to do.
To send data into fragment we should make instance/static function of that receiver fragment, and put all required params into this.
Then put data using fragment arguments into receiver fragment within instance function.
Finally just get those arguments into onCreate/onCreateView.
Note: This Fragment class was deprecated in API level 28.
Use the Jetpack Fragment Library Fragment for consistent behavior across all devices and access to Lifecycle.
It can also happen if you call requireContext() method before onCreate() of the fragment.
I just putted this overrided onCreate inside Myfragment, that extends Fragment. And it works. But i think, it's kind of a plug.
#Override
public void onCreate(Bundle savedInstanceState) {
MyFragment.newInstance();
Bundle b = new Bundle();
super.onCreate(b);
}
Make sure the fragment class is public as well

Best practice for instantiating a new Android Fragment

I have seen two general practices to instantiate a new Fragment in an application:
Fragment newFragment = new MyFragment();
and
Fragment newFragment = MyFragment.newInstance();
The second option makes use of a static method newInstance() and generally contains the following method.
public static Fragment newInstance()
{
MyFragment myFragment = new MyFragment();
return myFragment;
}
At first, I thought the main benefit was the fact that I could overload the newInstance() method to give flexibility when creating new instances of a Fragment - but I could also do this by creating an overloaded constructor for the Fragment.
Did I miss something?
What are the benefits of one approach over the other? Or is it just good practice?
If Android decides to recreate your Fragment later, it's going to call the no-argument constructor of your fragment. So overloading the constructor is not a solution.
With that being said, the way to pass stuff to your Fragment so that they are available after a Fragment is recreated by Android is to pass a bundle to the setArguments method.
So, for example, if we wanted to pass an integer to the fragment we would use something like:
public static MyFragment newInstance(int someInt) {
MyFragment myFragment = new MyFragment();
Bundle args = new Bundle();
args.putInt("someInt", someInt);
myFragment.setArguments(args);
return myFragment;
}
And later in the Fragment onCreate() you can access that integer by using:
getArguments().getInt("someInt", 0);
This Bundle will be available even if the Fragment is somehow recreated by Android.
Also note: setArguments can only be called before the Fragment is attached to the Activity.
This approach is also documented in the android developer reference: https://developer.android.com/reference/android/app/Fragment.html
The only benefit in using the newInstance() that I see are the following:
You will have a single place where all the arguments used by the fragment could be bundled up and you don't have to write the code below everytime you instantiate a fragment.
Bundle args = new Bundle();
args.putInt("someInt", someInt);
args.putString("someString", someString);
// Put any other arguments
myFragment.setArguments(args);
Its a good way to tell other classes what arguments it expects to work faithfully(though you should be able to handle cases if no arguments are bundled in the fragment instance).
So, my take is that using a static newInstance() to instantiate a fragment is a good practice.
There is also another way:
Fragment.instantiate(context, MyFragment.class.getName(), myBundle)
While #yydl gives a compelling reason on why the newInstance method is better:
If Android decides to recreate your Fragment later, it's going to call
the no-argument constructor of your fragment. So overloading the
constructor is not a solution.
it's still quite possible to use a constructor. To see why this is, first we need to see why the above workaround is used by Android.
Before a fragment can be used, an instance is needed. Android calls YourFragment() (the no arguments constructor) to construct an instance of the fragment. Here any overloaded constructor that you write will be ignored, as Android can't know which one to use.
In the lifetime of an Activity the fragment gets created as above and destroyed multiple times by Android. This means that if you put data in the fragment object itself, it will be lost once the fragment is destroyed.
To workaround, android asks that you store data using a Bundle (calling setArguments()), which can then be accessed from YourFragment. Argument bundles are protected by Android, and hence are guaranteed to be persistent.
One way to set this bundle is by using a static newInstance method:
public static YourFragment newInstance (int data) {
YourFragment yf = new YourFragment()
/* See this code gets executed immediately on your object construction */
Bundle args = new Bundle();
args.putInt("data", data);
yf.setArguments(args);
return yf;
}
However, a constructor:
public YourFragment(int data) {
Bundle args = new Bundle();
args.putInt("data", data);
setArguments(args);
}
can do exactly the same thing as the newInstance method.
Naturally, this would fail, and is one of the reasons Android wants you to use the newInstance method:
public YourFragment(int data) {
this.data = data; // Don't do this
}
As further explaination, here's Android's Fragment Class:
/**
* Supply the construction arguments for this fragment. This can only
* be called before the fragment has been attached to its activity; that
* is, you should call it immediately after constructing the fragment. The
* arguments supplied here will be retained across fragment destroy and
* creation.
*/
public void setArguments(Bundle args) {
if (mIndex >= 0) {
throw new IllegalStateException("Fragment already active");
}
mArguments = args;
}
Note that Android asks that the arguments be set only at construction, and guarantees that these will be retained.
EDIT: As pointed out in the comments by #JHH, if you are providing a custom constructor that requires some arguments, then Java won't provide your fragment with a no arg default constructor. So this would require you to define a no arg constructor, which is code that you could avoid with the newInstance factory method.
EDIT: Android doesn't allow using an overloaded constructor for fragments anymore. You must use the newInstance method.
Some kotlin code:
companion object {
fun newInstance(first: String, second: String) : SampleFragment {
return SampleFragment().apply {
arguments = Bundle().apply {
putString("firstString", first)
putString("secondString", second)
}
}
}
}
And you can get arguments with this:
val first: String by lazy { arguments?.getString("firstString") ?: "default"}
val second: String by lazy { arguments?.getString("secondString") ?: "default"}
I disagree with yydi answer saying:
If Android decides to recreate your Fragment later, it's going to call
the no-argument constructor of your fragment. So overloading the
constructor is not a solution.
I think it is a solution and a good one, this is exactly the reason it been developed by Java core language.
Its true that Android system can destroy and recreate your Fragment. So you can do this:
public MyFragment() {
// An empty constructor for Android System to use, otherwise exception may occur.
}
public MyFragment(int someInt) {
Bundle args = new Bundle();
args.putInt("someInt", someInt);
setArguments(args);
}
It will allow you to pull someInt from getArguments() latter on, even if the Fragment been recreated by the system. This is more elegant solution than static constructor.
For my opinion static constructors are useless and should not be used. Also they will limit you if in the future you would like to extend this Fragment and add more functionality to the constructor. With static constructor you can't do this.
Update:
Android added inspection that flag all non-default constructors with an error.
I recommend to disable it, for the reasons mentioned above.
I'm lately here. But somethings I just known that might help you a bit.
If you are using Java, there is nothing much to change. But for Kotlin developers, here is some following snippet I think that can make you a basement to run on:
Parent fragment:
inline fun <reified T : SampleFragment> newInstance(text: String): T {
return T::class.java.newInstance().apply {
arguments = Bundle().also { it.putString("key_text_arg", text) }
}
}
Normal call
val f: SampleFragment = SampleFragment.newInstance("ABC")
// or val f = SampleFragment.newInstance<SampleFragment>("ABC")
You can extend the parent init operation in child fragment class by:
fun newInstance(): ChildSampleFragment {
val child = UserProfileFragment.newInstance<ChildSampleFragment>("XYZ")
// Do anything with the current initialized args bundle here
// with child.arguments = ....
return child
}
Happy coding.
Best practice to instance fragments with arguments in android is to have static factory method in your fragment.
public static MyFragment newInstance(String name, int age) {
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putInt("age", age);
MyFragment fragment = new MyFragment();
fragment.setArguments(bundle);
return fragment;
}
You should avoid setting your fields with the instance of a fragment. Because whenever android system recreate your fragment, if it feels that the system needs more memory, than it will recreate your fragment by using constructor with no arguments.
You can find more info about best practice to instantiate fragments with arguments here.
Since the questions about best practice, I would add, that very often good idea to use hybrid approach for creating fragment when working with some REST web services
We can't pass complex objects, for example some User model, for case of displaying user fragment
But what we can do, is to check in onCreate that user!=null and if not - then bring him from data layer, otherwise - use existing.
This way we gain both ability to recreate by userId in case of fragment recreation by Android and snappiness for user actions, as well as ability to create fragments by holding to object itself or only it's id
Something likes this:
public class UserFragment extends Fragment {
public final static String USER_ID="user_id";
private User user;
private long userId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
userId = getArguments().getLong(USER_ID);
if(user==null){
//
// Recreating here user from user id(i.e requesting from your data model,
// which could be services, direct request to rest, or data layer sitting
// on application model
//
user = bringUser();
}
}
public static UserFragment newInstance(User user, long user_id){
UserFragment userFragment = new UserFragment();
Bundle args = new Bundle();
args.putLong(USER_ID,user_id);
if(user!=null){
userFragment.user=user;
}
userFragment.setArguments(args);
return userFragment;
}
public static UserFragment newInstance(long user_id){
return newInstance(null,user_id);
}
public static UserFragment newInstance(User user){
return newInstance(user,user.id);
}
}
use this code 100% fix your problem
enter this code in firstFragment
public static yourNameParentFragment newInstance() {
Bundle args = new Bundle();
args.putBoolean("yourKey",yourValue);
YourFragment fragment = new YourFragment();
fragment.setArguments(args);
return fragment;
}
this sample send boolean data
and in SecendFragment
yourNameParentFragment name =yourNameParentFragment.newInstance();
Bundle bundle;
bundle=sellDiamondFragments2.getArguments();
boolean a= bundle.getBoolean("yourKey");
must value in first fragment is static
happy code
Ideally we shouldn't pass anything in the fragment constructor, fragment constructor should be empty or default.
Now the second question is, what if we want to pass interface variable or parameters-
We should use Bundle to pass data.
For Interface we can putParceble in bundle and make that interface implement parceble
If possible we can implement that interface in activity and in fragment we can initialize listener in OnAttach where we have context[ (context) Listener].
So that during configuration change (e.g. Font change) the Activity recreation listener won't go uninitialize and we can avoid a null pointer exception.
Best way to instantiate the fragment is use default Fragment.instantiate method or create factory method to instantiate the the fragment
Caution: always create one empty constructor in fragment other while restoring fragment memory will throw run-time exception.
You can use smth like this:
val fragment = supportFragmentManager.fragmentFactory.instantiate(classLoader, YourFragment::class.java.name)
because this answer now is Deprecated
Create an instance of the fragment using kotlin code.
Write in activity
val fragment = YourFragment.newInstance(str = "Hello",list = yourList)
Write in fragment
fun newInstance(str: String, list: ArrayList<String>): Fragment {
val fragment = YourFragment()
fragment.arguments = Bundle().apply {
putSerializable("KEY_STR", str)
putSerializable("KEY_LIST", list)
}
return fragment
}
Using the same fragment retrieve the data from bundle
val str = arguments?.getString("KEY_STR") as? String
val list = arguments?.getSerializable("KEY_LIST") as? ArrayList<String>
setArguments() is useless. It only brings a mess.
public class MyFragment extends Fragment {
public String mTitle;
public String mInitialTitle;
public static MyFragment newInstance(String param1) {
MyFragment f = new MyFragment();
f.mInitialTitle = param1;
f.mTitle = param1;
return f;
}
#Override
public void onSaveInstanceState(Bundle state) {
state.putString("mInitialTitle", mInitialTitle);
state.putString("mTitle", mTitle);
super.onSaveInstanceState(state);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
if (state != null) {
mInitialTitle = state.getString("mInitialTitle");
mTitle = state.getString("mTitle");
}
...
}
}
I believe I have a much simpeler solution for this.
public class MyFragment extends Fragment{
private String mTitle;
private List<MyObject> mObjects;
public static MyFragment newInstance(String title, List<MyObject> objects)
MyFragment myFrag = new MyFragment();
myFrag.mTitle = title;
myFrag.mObjects = objects;
return myFrag;
}

Categories

Resources