Introduction to FragmentArgs in android to facilitate writing parameters for fragments

Android development can sometimes be a headache. You have to write a lot of code for something as simple as creating fragments. Fortunately, java supports a powerful tool: Annotation Processors.

The problem with Fragment is that you have to set a lot of parameters to make it work. Many new android developers usually write this:

01
public class MyFragment extends Fragment
02
{
03
private int id;
04
private String title;
05

06
public static MyFragment newInstance(int id, String title)
07
{
08
MyFragment f = new MyFragment();
09
f.id = id;
10
f.title = title;
11
return f;
12
}
13

14
@Override
15
public View onCreateView(LayoutInflater inflater, ViewGroup container,
16
Bundle savedInstanceState)
17
{
18
Toast.makeText(getActivity(), "Hello " + title.substring(0, 3),
19
Toast.LENGTH_SHORT).show();
20
}
21
}
What's wrong with doing this? I have tried it on my own device and it works fine?


It does work, but have you tried changing your device from portrait to landscape? Your app will crash with NullPointerException when you try to access id or title.

My app is normal because I set the app to portrait orientation. So I never had this problem.

up to you! Android is a true multitasking operating system. Multiple apps are running at the same time, and the Android system will destroy the activity (and the fragments it contains) if memory is needed. Maybe you don't pay attention to these problems in your daily app development. However, when you publish in the play store, you will notice that your app crashes, but you don't know why. Users of your app may be using multiple apps at the same time, and it is very likely that your app will be destroyed in the background. For example: User A opens your app, MyFragment is displayed on the screen. Next your user presses the home button (this is your app running in the background) and opens other apps. Your app may be destroyed by freeing memory. Afterwards, the user returns to your app, e.g. via a multitasking button. So, what will Android do now? Android restores the previous app state, and at the same time restores MyFragment, that's the problem. Fragment tries to access title, but title is null because it is not persisted.

I got it, so I need to save them in onSaveInstanceState(Bundle)?

no. The official documentation is a bit unclear, but onSaveInstanceState(Bundle) should be used in the same way as Activity.onSaveInstanceState(Bundle) : you use this method to save the "temporary" state of the instance, for example to handle the orientation of the screen (from portrait orientation) to landscape and vice versa). Therefore, when the app is killed in the background, the instance state of the fragment cannot be saved as persistent data. Its function is to restore the data when it returns to the foreground again. Its role is the same as Activity.onSaveInstanceState(Bundle) in Activity, they are used to "temporarily" save instance state. However, persistent parameters are transferred via intent external data.

So I should have the Intent in the Activity to save the Fragment's parameters?

No, Fragment has its own mechanism. There are two methods: Fragment.setArguments(Bundle) and Fragment.getArguments(), which you must use to ensure that the arguments are persisted. That's the pain I mentioned above. There needs to be a lot of code written this way. First, you need to create a Bundle, then you need to put in key-value pairs, and finally call Fragment.setArguments(). Unfortunately, your work isn't over yet, you have to read out the Bundle via Fragment.getArguments(). Some such work:
Click here to read the full text: http://click.aliyun.com/m/22137/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309812&siteId=291194637