It's time to get to know Android7: multi-window support

Source: Internet
Author: User
Tags gettext

This article has authorized the public number: Yang (hongyangandroid) in the public platform original debut.

At the beginning of this article, let's look at some of the new features of Android 7, saying that this year Android 7 preview has come a little earlier than before, so it's good news for our developers, and we have plenty of time to look at some of the features of the new Android. Let our app faster support to Android 7. Some time ago, Android 7 sent the final preview version, which also means that the SDK is now the final SDK, so from now on, we can fully enable the app to support Android 7.

Today's article let's introduce one of the most intuitive features on Android 7-Multi-window support, of course, you can also call him split-screen mode. With this feature, MOM doesn't have to worry about my switching between apps anymore, so what's the multi-window mode? In fact, many domestic machines have already supported many windows, but this time the Android 7 standardized multi-window mode, which for our developers, can be regarded as great news. That's nonsense, we haven't seen the multi-window mode yet. Here's a picture to try.

Let our app support multi-window mode

How do we enable our app to support multi-window mode? In fact, Android 7 turns on the Multi-window mode by default, but if you build an app with an SDK below Android 7, a warning will be issued in multi-window mode. So how do we disable the Multi-window mode for our app? After all, many people still do not like to let their application and others to share the screen (such as QQ), this is also very simple, just need to add in the application or activity of the manifest file android:resizeableActivity="false" is OK (visual, the next most domestic apps will have this attribute).

Some configurations for multi-window mode

Disable is disabled, but for our developers, still want to continue to understand the multi-window mode, then we look at, in the Multi-window mode will be more properties. Where we configure activity in the manifest file, there is one more layout node, this node's properties are not many, mainly used to configure some properties in the Multi-window mode. Let's start by looking at how to configure, and then say what the effect is:

<activity android:name=".MyActivity">    <layout          android:defaultHeight="500dp"          android:defaultWidth="500dp"          android:gravity="bottom|end"          android:minimalHeight="200dp"          android:minimalWidth="200dp" /></activity>

Quite simply, there is one more layout node, let's take a look at his attributes.

  1. android:defaultHeightThis is the default height for configuring multi-window mode.
  2. android:defaultWidthThis is the default width for configuring multi-window mode.
  3. android:gravityThis is the initial position of the activity in configuring the multi-window mode. ( Note: This statement found that it did not work during my test )
  4. android:minimalHeightThis is the smallest height configured in multi-window mode. ( Note: This statement will not compile directly after I have found the configuration in my test )
  5. android:minimalWidthThis is the smallest width configured in multi-window mode. ( Note: This statement will not compile directly after I have found the configuration in my test )

In fact, even if our application to support multi-window mode, the above layout node we can be completely ignored (and I feel most of the situation is to ignore)

Or look at the life cycle.

In fact, the multi-window itself is very simple, we are most concerned about activity in the Multi-window mode of life cycle, below we use a demo to look at the activity in the Multi-window mode of life cycle.

@Overrideprotected void onCreate(Bundle savedinstancestate) {Prntlog ("OnCreate");}@Overrideprotected void OnStart() {Prntlog ("OnStart");Super. OnStart ();}@Overrideprotected void Onresume() {Prntlog ("Onresume");Super. Onresume ();}@Overrideprotected void OnPause() {Prntlog ("OnPause");Super. OnPause ();}@Overrideprotected void OnStop() {Prntlog ("OnStop");Super. OnStop ();}@Overrideprotected void OnDestroy() {Prntlog ("Ondestory");Super. OnDestroy ();}@Overrideprotected void onsaveinstancestate(Bundle outstate) {Prntlog ("Onsaveinstancestate");Super. Onsaveinstancestate (outstate);}@Overrideprotected void onrestoreinstancestate(Bundle savedinstancestate) {Prntlog ("Onrestoreinstancestate");Super. Onrestoreinstancestate (savedinstancestate);}@Override Public void onmultiwindowmodechanged(BooleanIsinmultiwindowmode) {Prntlog ("onmultiwindowmodechanged:"+ Isinmultiwindowmode); Prntlog ("Isinmultiwindowmode:"+ Isinmultiwindowmode ());Super. onmultiwindowmodechanged (Isinmultiwindowmode);}Private void Prntlog(String log) {LOG.D ("Mainactivity", log);}

Before we start, we find that there is a callback that onMultiWindowModeChanged we are not familiar with, this callback is for the new one in the Multi-window mode, the callback will execute when entering or exiting the Multi-window mode, and we can also use the isInMultiWindowMode() method to determine whether the activity is in multi-window mode. Next, let's demonstrate the life cycle.

The first is to enter the multi-window mode (the way to enter the multi-window mode is to long press the overview key on the phone)

D/MainActivity: onMultiWindowModeChanged:trueD/MainActivity: isInMultiWindowMode:trueD/MainActivity: onPauseD/MainActivity: onSaveInstanceStateD/MainActivity: onStopD/MainActivity: onDestoryD/MainActivity: onCreateD/MainActivity: onStartD/MainActivity: onRestoreInstanceStateD/MainActivity: onResumeD/MainActivity: onPause

As you can see from log, the first callback is the method when entering the multi-window mode, onMultiWindowModeChanged and then it is frustrating that our activity is destroyed, and the method is called, onSaveInstanceState and then Activit starts, which is actually our activity reboot.

What about exiting the multi-window mode?

D/MainActivity: onSaveInstanceStateD/MainActivity: onStopD/MainActivity: onDestoryD/MainActivity: onCreateD/MainActivity: onStartD/MainActivity: onRestoreInstanceStateD/MainActivity: onResumeD/MainActivity: onPauseD/MainActivity: onMultiWindowModeChanged:falseD/MainActivity: isInMultiWindowMode:falseD/MainActivity: onResume

The first is a configuration change destruction process, then a recovery process, and callback onMultiWindowModeChanged method, at this time isInMultiWindowMode is false.

Keep looking at the life cycle, what if our focus shifts from one activity to another activity with it in multi-window mode?

D/MainActivity: onPauseD/SecondActivity: onResume

At this time the activity will be paused, the new acquisition angle of activity callback, there is onResume a notice on the official website, for example, we were in the onPause pause video playback, in this case, the loss of focus after the pause, obviously this is not a good user experience, We need to put the video pause and continue onStop in and out onStart .

Start activity

Now we're here to learn how to start activity in multi-window mode. In two cases, one is started in the current stack, and the other is started 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 that it should be started under the sibling window, just set a flag to intent FLAG_ACTIVITY_LAUNCH_ADJACENT .
For example, the following code:

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

The code above will start a new activity in another window.

In addition, we can also set the size of the newly started activity.

new Rect(5003001000new Intent(this, SecondActivity.class);intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT|Intent.FLAG_ACTIVITY_NEW_TASK);ActivityCompat.startActivity(this, intent, options.toBundle());
Drag across Activity

Since Android 4.0, Android has supported content drag and drop in the activity, now in multi-window mode, Android enhanced drag and drop, and in multi-window mode can be in the activity between the content of the drag and drop, However, in the activity is limited to the content of the drag and drop on the view of the cross-activity is not possible. Now let's simulate the cross-activity drag-and-drop content before mainactivity and secondactivity.

//MainActivity  Button view = (Button) Findviewbyid (R.id.button), View.setonlongclicklistener (new  View.onlongclicklistener () { @Override  public
      boolean  onlongclick  (view view) {Clipdata D        ATA = 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 for the button's long-press event, and in the long-press event, we first construct an object with the text of the button ClipData . Then invoke the view.startDragAndDrop method to start the drag-and-drop. Note the last parameter here View.DRAG_FLAG_GLOBAL , This flag indicates that we can drag and drop across the activity.

Then let's look at how Secondactivity handles drag and drop events.

FinalTextView content = (TextView) Findviewbyid (r.id.content); Findviewbyid (R.id.container). Setondraglistener (NewView.ondraglistener () {@Override Public Boolean Ondrag(View view, Dragevent dragevent) {Switch(Dragevent.getaction ()) { CaseDragEvent.ACTION_DRAG_STARTED:prntLog ("Drag started"); Break; CaseDragEvent.ACTION_DRAG_ENTERED:prntLog ("Drag entered"); Break; CaseDragEvent.ACTION_DROP:ClipData.Item Item = Dragevent.getclipdata (). Getitemat (0); Content.settext (Item.gettext ()); Break; CaseDragEvent.ACTION_DRAG_ENDED:prntLog ("Drag entered"); Break; }return true;}});

Here first we get the root layout (here is not the root layout in the end who, here is the root layout refers to the root layout of the Content_view), and then to set it to OnDragListener listen, at the ACTION_DROP time we get through the dragEvent.getClipData().getItemAt(0) dragged item, and then through getText() The method gets to the content and is set to TextView on display.
Look at the effect:

OK, so far the multi-window mode of Android 7 we have finished, these content we have a little impression on it, after all, in our daily work in the basic one android:resizeableActivity="false" can be.

It's time to get to know Android7: multi-window support

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.