1. The difference between the two onCreate() methods

The Activity’s onCreate() method has two 1’s. A protected 2’s. The other one is public how can we have two onCreate here that we can overload? People with dyspraxia say they can’t choose at all. WTF? Long experience tells us that we’re going to override the first onCreate method, which has only one parameter and always has only one parameter. And constrain it with protected.

The difference between the two methods: We know that onCreate is the first method in the Activity lifecycle. We usually do some View initializations in onCreate, etc. OnCreate only initializes once in its lifetime, but it also has an important function: when our Activity is destroyed abnormally, such as when the phone rotates, or when the background is automatically destroyed due to insufficient memory. To protect our data, we can save our data in savedInstanceState, which will not disappear when the Activity restarts. We can retrieve our data using the savedInstance parameter in the onCreate method. It’s pretty easy to do that by just overloading onSaveInstanceState or onRestoreInstance

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}
Copy the code
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}
Copy the code

The method passes in the data as key-value pairs and provides a Bundle outState parameter to hold the data temporarily. Then fetch it from onCreate, ensuring that the Activity is destroyed but the data is not. It is really a simple and practical function to solve the majority of mobile phone memory shortage and the user front desk data loss pain point.

The onCreate method is powerful enough, but can it be more powerful? Is there a case where the phone is overheating, running out of power or customized by a third party? Here is the second method of onCreate

@Override
public void onCreate(@Nullable Bundle savedInstanceState,
 @Nullable PersistableBundle persistentState) {
      super.onCreate(savedInstanceState, persistentState);
  }
Copy the code

When your phone shuts down unexpectedly, he can retrieve the data from the previous front desk. It is actually a data persistence Activity

PersistableMode = “persistAcrossReboots” we need to specify the following properties in the Android manifest Activity: Android :persistableMode= “persistAcrossReboots”

Then reload onSaveInstanceState or onRestoreInstance:

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

@Override
public void onRestoreInstanceState(Bundle savedInstanceState, 
PersistableBundle persistentState) {
	    super.onRestoreInstanceState(savedInstanceState, persistentState);
}
 
Copy the code

They correspond to a persistentState of type PersistableBundle. It’s OK to align it.

Add: it says reload onSaveInstanceState or onRestoreInstance. To explain these two methods, onSaveInstanceState is called when the current Activity is about to be destroyed but not yet. When the system calls onRestoreInstance, the Activity is destroyed. You can look at the demand

Call, here’s a little bit of knowledge.

2.setContentView

The code below setContentView is familiar to every Androider, and beginners will forget it when building a new Acitivity, resulting in an error

3. Add scroll bars to your text

Add a scroll bar to text content TextView TV;

tv.setMovementMethod(ScrollingMovementMethod.getInstance());
Copy the code

4. The public key

We can create multiple objects based on a class, each with its own members, independent of each other. At some point, however, we prefer that all objects of that class share the same member. This is where static comes in

Members decorated static in Java are called static members or class members. It is owned by the class as a whole, not by an object, that is, shared by all objects of the class. Static members can be accessed directly using the class name or by using the object name. Of course, class name access is preferred because of the nature of its use. Static can be used to modify variables, methods, and code blocks.Copy the code

We define a static variable called Hobby in our class, which looks like this:

Note: Static members belong to the entire class and are allocated memory when the class is first used until it is uninstalled!Copy the code

7. ScrollView nested ListView bug

It’s a bad idea to have a scrollView nested with a ListView, so it’s better to use a listView, Add the other parts that need to be slid into the head layout and foot board or use a flag bit to identify the head and foot as a separate item in the Adapter, so that the performance is best and bugs are not easy to occur.

Of course, it is inevitable that someone will use it, and there will be a bug that shows only one item. There are two ways to solve this problem. One is to create a custom WrapContentListview that simply inherits from the Listview and overrides the OnMeasure method

public class WrapContentListView extends ListView{ public WrapContentListView(Context context) { super(context); } public WrapContentListView(Context context, AttributeSet attrs) { super(context, attrs); } public WrapContentListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); }} //Copy the code

8. The android: descendantFocusability usage analysis

A very common problem in development, project ListView is not just simple text, often need to define their own Adapter ListView to inherit BaseAdapter, in Adapter according to the requirements of writing, The problem is that when you click on each item there’s no response and you can’t get the focus. The reason for this is that there are child controls such as ImageButton, Button, and CheckBox in the Item you define, and these child controls capture the focus of the Item. So often what happens when you click on an item is that the child control item itself doesn’t respond to the click.

This property is solved with descendantFocusability, which defines the relationship between the viewGroup and its descendantFocusability when a view gets focus.

  • There are three types of values for attributes:

    BeforeDescendants: A viewgroup takes precedence over its subclass controls to get focus

    AfterDescendants: A viewgroup gets focus only when its subclass controls do not need it

    BlocksDescendants: Viewgroup overrides subclass controls to get focus directly

  • ListView uses the following:

9.TextView uses Html tags in the code to set up a line of fonts to display different colors

String statusHtml = String. Format (locale.china," <font color=#21bd5f>%s</font>", status2statusName(item.getStatus())); TextView statusView = helper.getView(R.id.tv_apply_for_status); statusView.setText(Html.fromHtml(statusHtml));Copy the code

10. Optimization of submitting Button or TextView during data submission to prevent users from clicking excessively

Submitbtn.setenabled (networkState.getStatus()! = NetworkState.Status.RUNNING); submitBtn.setText(networkState.getStatus() == NetworkState.Status.RUNNING ? "Submitted..." : getString(R.string.submit));Copy the code

11. Use Handler to implement the timer

new Handler().postDelayed(new Runnable() { @Override public void run() { switch (par) { case 1: Callback.onsuccess (" loading successfully "); break; Case 2: callback.onFail(" load failed "); break; default: callback.onError(); break; } // end callback.oncomplete (); } // Delay two seconds}, 2000);Copy the code

12. The split in the Java () special symbol “, “” |” * “” “” “”

The dot problem is solved with string.split("[.]"). Questions about the vertical lines in the string. The split (" \ \ | "). The problem with asterisks is solved with string.split("\\*"). The slash problem is solved with sring.split("\\\\"). Sring.split ("\\[\]") is used to solve the problem of parentheses.Copy the code

13.TextView text attribute

This property is used to display a line of text. If there are too many cases, ellipses should be used instead of Android: Ellipsize ="end". This property can be used with Android :maxLines="1".Copy the code

14. About the problem of reserving two decimal places for double values

Public double getGoodsRealPrice() {return goodsRealPrice; } // We can write a method below to keep two decimal places and then apply! public String doubleToString() { return String.format(Locale.CHINA, "%.2f", goodsRealPrice); }Copy the code

15. Two side-by-side TextViews with inconsistent font sizes require alignment at the bottom of the font

Use the attribute of ConstraintLayout app:layout_constraintBaseline_toBaselineOf="@+ ID /money_sign" Money_sign is the ID of the component to alignCopy the code

16. Hidden click effect on the GridView item

SetClickable (false); // Set GridView unclickable when initializing GridView. gridview.setPressed(false); gridview.setEnabled(false);Copy the code

I want to work my way up

Fly forward on the blades at the highest point

Let the wind blow dry tears and sweat

I want to work my way up

Waiting for the sun to watch its face

Little days have big dreams

I have my day

Let the wind blow dry tears and sweat

One day I will have my day