Time to learn about android7: multi-window support

At the beginning of this article, let's take a look at some new features of Android 7. In other words, this year's android 7 preview version came a little earlier than before, which is good news for us developers, we can have enough time to come Looking at some features of the new version of android, our applications can support android 7 faster. Some time ago, android 7 sent the final preview version, which also means that the current sdk is already the final sdk, so we can start from now on. Let the application support android 7.

In today's article, let's introduce one of the most intuitive features on android 7 - multi-window support, of course, it can also be called split-screen mode. With this feature, mother no longer has to worry about me The trouble of switching between applications, what about multi-window mode? In fact, many domestic machines already support multi-window mode, but this time android 7 standardizes multi-window mode, which can be regarded as a multi-window mode for our developers. Great news. Nonsense, so we haven't seen what multi-window mode looks like? Let's experience it with a picture below.


Let our application support multi-window mode

How to make our application support multi-window mode? Actually Android 7 enables multi-window mode by default, but if you build an application with an sdk lower than android 7, a warning will be issued in multi-window mode. So how do we disable multi-window mode for our application? After all, many people still If you don't like to let your application share the screen with others (such as QQ), this is also very simple. You only need to add android:resizeableActivity="false" to the application or activity of the manifest file. There will be this property in APP).

Some configurations of multi-window mode

Disabled is disabled, but for us developers, we still have to continue to understand multi-window mode, then let's take a look at what properties are added in multi-window mode. In the manifest file, where we configure the activity , there is one more layout node, this node has few properties, it is mainly used to configure some properties in multi-window mode. Let's first take a look at how to configure it, and then talk about what it does:
<activity android:name=".MyActivity">
    <layout
          android:defaultHeight="500dp"
          android:defaultWidth="500dp"
          android:gravity="bottom|end"
          android:minimalHeight="200dp"
          android:minimalWidth="200dp" />
</activity>

It's very simple, just one more layout node, let's take a look at its properties.
  • android:defaultHeight This is to configure the default height in multi-window mode.
  • android:defaultWidth This is to configure the default width in multi-window mode.
  • android:gravity This is to configure the initial position of the activity in multi-window mode. (Note: This statement seems to have no effect during my testing)
  • android:minimalHeight This is to configure the minimum height in multi-window mode. (Note: This statement cannot be compiled directly after the configuration is found during my testing)
  • android:minimalWidth This is to configure the minimum width in multi-window mode. (Note: This statement can't be compiled directly after the configuration is found during my testing)

In fact, even if our application supports multi-window mode, we can completely ignore the layout node above (and I think it should be ignored in most cases)

or look at the life cycle.

In fact , the multi-window itself is still very simple, What we are most concerned about is the life cycle of activity in multi-window mode. Let's use a demo to see the life cycle of activity in multi-window mode.
@Override
protected void onCreate(Bundle savedInstanceState) {
    prntLog("onCreate");
}

@Override
protected void onStart() {
    prntLog("onStart");
    super.onStart();
}

@Override
protected void onResume() {
    prntLog("onResume");
    super.onResume();
}

@Override
protected void onPause() {
    prntLog("onPause");
    super.onPause();
}

@Override
protected void onStop() {
    prntLog("onStop");
    super.onStop();
}

@Override
protected void onDestroy() {
    prntLog("onDestory");
    super.onDestroy ();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    prntLog("onSaveInstanceState");
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    prntLog("onRestoreInstanceState");
    super.onRestoreInstanceState (savedInstanceState);
}

@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
    prntLog("onMultiWindowModeChanged:" + isInMultiWindowMode);
    prntLog("isInMultiWindowMode:" + isInMultiWindowMode());
    super.onMultiWindowModeChanged(isInMultiWindowMode);
}

private void prntLog(String log) {
    Log.d("MainActivity", log);
}

Before starting, we found that there is a callback onMultiWindowModeChanged that we are not familiar with. This callback is a new one for multi-window mode. When entering or exiting multi-window mode, this callback will be executed. Moreover, we can also use isInMultiWindowMode () method to determine whether the current activity is in multi-window mode. Next, let's demonstrate the life cycle. The

first is to enter multi-window mode (the way to enter multi-window mode is to long press the overview button on the phone)
D/MainActivity: onMultiWindowModeChanged:true
D/MainActivity: isInMultiWindowMode:true
D/MainActivity: onPause
D/MainActivity: onSaveInstanceState
D/MainActivity: onStop
D/MainActivity: onDestory
D/MainActivity: onCreate
D/MainActivity: onStart
D/MainActivity: onRestoreInstanceState
D/MainActivity: onResume
D/MainActivity: onPause

It can be found from the log that when entering multi-window mode, the onMultiWindowModeChanged method is first called back, and then it is very frustrating that our activity is destroyed, and the onSaveInstanceState method is called, and then activit starts, which is actually our activity restarts.

What about exiting multi-window mode?
D/MainActivity: onSaveInstanceState
D/MainActivity: onStop
D/MainActivity: onDestory
D/MainActivity: onCreate
D/MainActivity: onStart
D/MainActivity: onRestoreInstanceState
D/MainActivity: onResume
D/MainActivity: onPause
D/MainActivity: onMultiWindowModeChanged:false
D/MainActivity: isInMultiWindowMode:false
D/MainActivity: onResume

The first is a configuration change destruction process, and then a recovery process, and the onMultiWindowModeChanged method is called back. At this time, isInMultiWindowMode is false.

Continue to look at the life cycle. If our focus is switched from an activity to a multi-window with it What about another activity in the mode?
D/MainActivity: onPause
D/SecondActivity: onResume

At this point, the current activity will be paused, and the newly acquired activity will call back onResume. There is also a notice on the official website. For example, we previously paused the video playback in onPause. In this case, it will pause after losing focus. Obviously, this is not For a good user experience, we need to put the pause and resume of the video into onStop and onStart.

Start the activity

Now let's learn how to start the activity in multi-window mode. There are two cases, one is the current Start in the stack, the other is to start in a new stack.

For the first case, it is very simple to start a new activity in the current window, and in the second case, we can specify to start in the same level window, Just set a FLAG_ACTIVITY_LAUNCH_ADJACENTflag to the intent and it's ok.

For example, the following code:
Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

In the above code, we will start a new activity in another window.

In addition, we can also specify the size of the newly started activity.
Rect bounds = new Rect(500, 300, 100, 0);
ActivityOptions options = ActivityOptions.makeBasic();
options.setLaunchBounds(bounds);

Intent intent = new Intent(this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT|Intent.FLAG_ACTIVITY_NEW_TASK);
ActivityCompat.startActivity(this, intent, options.toBundle());


Drag and drop across activities

Since android 4.0, android has supported the drag and drop of content within activities. Now in multi-window mode, android has enhanced the drag-and-drop function, and in multi-window mode, content can be dragged between activities. Drag, but it is limited to dragging content between activities, and dragging and dropping of views across activities is still not possible. Now let's simulate dragging content across activities before MainActivity and SecondActivity.
// MainActivity
Button view = (Button) findViewById(R.id.button);
view.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        ClipData data = ClipData.newPlainText(view.getClass().getName(),
                ((Button) view).getText());
        View.DragShadowBuilder builder = new View.DragShadowBuilder(view);
        view.startDragAndDrop(data, builder, view, View.DRAG_FLAG_GLOBAL);
        return true;
    }
});

Here we listen to the long press event of the button. In the long press event, first we build a ClipData object with the text of the button. Then call the view.startDragAndDrop method to start dragging. Note the last parameter View.DRAG_FLAG_GLOBAL, this flag Indicates that we can drag and drop across activities.

Let's take a look at how SecondActivity handles drag events.
final TextView content = (TextView) findViewById(R.id.content);
findViewById(R.id.container).setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
    switch (dragEvent.getAction()) {
        case DragEvent.ACTION_DRAG_STARTED:
            prntLog("drag started");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            prntLog("drag entered");
            break;
        case DragEvent.ACTION_DROP:
            ClipData.Item item = dragEvent.getClipData().getItemAt(0);
            content.setText(item.getText());
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            prntLog("drag entered");
            break;
    }
    return true;
}
});

Here first we get the root layout (don't worry about who the root layout is here, the root layout here refers to the root layout of content_view), and then set the OnDragListener monitor for it, and at ACTION_DROP, we pass dragEvent.getClipData( ).getItemAt(0) to get the dragged item, then get the content through the getText() method, and set it to display on the TextView. Let's

see the effect:

ok, so far we have finished introducing the multi-window mode of android 7 , You can have a little impression of these contents. After all, in our daily work, basically an android:resizeableActivity="false" is enough.

Guess you like

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