1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.documentcentricapps; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.PersistableBundle; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.CheckBox; 26 27 /** 28 * DocumentCentricActivity shows the basic usage of the new Document-Centric Apps API. The new 29 * API modifies the meaning of the {@link Intent#FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET} flag, which is 30 * now deprecated. In versions before L it serves to define a boundary between the main task and a 31 * subtask. The subtask holds a different thumbnail and all activities in it are finished when the 32 * task is reset. In L this flag causes a full break with the task that launched it. As such it has 33 * been renamed to {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT}. 34 * 35 * This sample mainly uses Intent flags in code. But Activities can also specify in their manifests 36 * that they shall always be launched into a new task in the above manner using the new activity 37 * attribute documentLaunchMode which may take on one of three values, “intoExisting” equivalent to 38 * NEW_DOCUMENT, “always” equivalent to NEW_DOCUMENT | MULTIPLE_TASK, “none” the default, and 39 * “never” which will negate the effect of any attempt to launch the activity with NEW_DOCUMENT. 40 */ 41 public class DocumentCentricActivity extends Activity { 42 43 private final static String TAG = "DocumentCentricActivity"; 44 45 public final static String KEY_EXTRA_NEW_DOCUMENT_COUNTER = "KEY_EXTRA_NEW_DOCUMENT_COUNTER"; 46 47 private static int mDocumentCounter = 0; 48 49 private CheckBox mCheckbox; 50 51 @Override onCreate(Bundle savedInstanceState)52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.activity_document_centric_main); 55 mCheckbox = (CheckBox) findViewById(R.id.multiple_task_checkbox); 56 } 57 58 @Override onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState)59 public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 60 super.onPostCreate(savedInstanceState, persistentState); 61 // Restore state from PersistableBundle 62 if (persistentState != null) { 63 mDocumentCounter = persistentState.getInt(KEY_EXTRA_NEW_DOCUMENT_COUNTER); 64 } 65 } 66 67 @Override onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)68 public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 69 /* 70 To maintain activity state across reboots the system saves and restore critical information for 71 all tasks and their activities. Information known by the system includes the activity stack order, 72 each task’s thumbnails and each activity’s and task's Intents. For Information that cannot be retained 73 because they contain Bundles which can’t be persisted a new constrained version of Bundle, 74 PersistableBundle is added. PersistableBundle can store only basic data types. To use it 75 in your Activities you must declare the new activity:persistableMode attribute in the manifest. 76 */ 77 outPersistentState.putInt(KEY_EXTRA_NEW_DOCUMENT_COUNTER, mDocumentCounter); 78 super.onSaveInstanceState(outState, outPersistentState); 79 } 80 createNewDocument(View view)81 public void createNewDocument(View view) { 82 boolean useMultipleTasks = mCheckbox.isChecked(); 83 final Intent newDocumentIntent = newDocumentIntent(); 84 if (useMultipleTasks) { 85 /* 86 When {@linkIntent#FLAG_ACTIVITY_NEW_DOCUMENT} is used with {@link Intent#FLAG_ACTIVITY_MULTIPLE_TASK} 87 the system will always create a new task with the target activity as the root. This allows the same 88 document to be opened in more than one task. 89 */ 90 newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK); 91 } 92 startActivity(newDocumentIntent); 93 } 94 95 96 /** 97 * Returns an new {@link Intent} to start {@link NewDocumentActivity} as a new document in 98 * overview menu. 99 * 100 * To start a new document task {@link Intent#FLAG_ACTIVITY_NEW_DOCUMENT} must be used. The 101 * system will search through existing tasks for one whose Intent matches the Intent component 102 * name and the Intent data. If it finds one then that task will be brought to the front and the 103 * new Intent will be passed to onNewIntent(). 104 * 105 * Activities launched with the NEW_DOCUMENT flag must be created with launchMode="standard". 106 */ newDocumentIntent()107 private Intent newDocumentIntent() { 108 final Intent newDocumentIntent = new Intent(this, NewDocumentActivity.class); 109 newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT); 110 newDocumentIntent.putExtra(KEY_EXTRA_NEW_DOCUMENT_COUNTER, incrementAndGet()); 111 return newDocumentIntent; 112 } 113 incrementAndGet()114 private static int incrementAndGet() { 115 Log.d(TAG, "incrementAndGet(): " + mDocumentCounter); 116 return mDocumentCounter++; 117 } 118 119 } 120