Wednesday, March 6, 2019

Android开发笔记-ch3.2.4 Dialog; 3.2.5 Fragments


3.2.4 Dialog

Refer android for general info. Refer to this SO where has several example code for using alert builder to create Alert dialog, which has positive and negative choices, layout xml is optional. There is no modal style dialog, refer to this SO. setCancelable(false) doesn't work.

3.2.5 Fragments

Refer to Android develop guide of Fragments. Fragments is introduced since Android 3.0 (API level 11). A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running.
As mentioned there, two ways to add a fragment to Activity layout:
  • Declare the fragment inside the activity's layout xml file
  • programmatically add the fragment to an existing ViewGroup
For the 2nd way: At any time while your activity is running, you can add fragments to your activity layout. You simply need to specify a ViewGroup in which to place the fragment. To make fragment transactions in your activity (such as add, remove, or replace a fragment), you must use APIs from FragmentTransaction. You can get an instance of FragmentTransaction from your Activity like this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
You can then add a fragment using the add() method, specifying the fragment to add and the view in which to insert it.
If override onCreate(), make sure invoke super.onCreate() to avoid exception, this is required by both Activity and Fragment.
In onCreateView(), have to use the view to find widget like this(otherwise it will return null):
View rootView = inflater.inflate(R.layout.fragment_myaccount, container, false);
mSpinner_account = (Spinner) rootView.findViewById(R.id.spinner_account);
For Fragment vs. FragmentActivity, refer to this SO. FragmentActivity is Activity with fragment support prior Android 3.0 HoneyComb. For Fragment, it has to be embedded to an Activity. According to Fragment document: All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore. Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments(). Subclass of Fragment usually would be a nested class of main Activity class, and it should be static nested class. Refer to this SO. My understanding is: since fragment can be re-instantiated at any time, it cannot hold any reference to the enclosing main Activity class' non-static member. So it cannot be inner class. And if fragment needs to access a member of MainActivity class, the member has to be static of the MainActivity class, or use setArguments/getArguments as mentioned above.
The framework may re-instantiate a fragment class when orientation changed, or new fragment is added or a fragment is removed.

0 Comments:

Post a Comment