IntentPlayground start activities for result

Test: Manual
Change-Id: I3004e6dd76c026e41d2c9a28f4dd59664f99cd09
This commit is contained in:
Liam Clark
2019-01-07 11:28:23 -08:00
parent 5e9e4f94aa
commit ba9b6ca212
8 changed files with 70 additions and 88 deletions

View File

@@ -50,6 +50,7 @@ public abstract class BaseActivity extends AppCompatActivity implements
public static final String TREE_FRAGMENT = "com.example.android.treeFragment";
public static final String EXPECTED_TREE_FRAGMENT = "com.example.android.expectedTreeFragment";
public static final int LAUNCH_REQUEST_CODE = 0xEF;
private static final int LAUNCH_FOR_RESULT_ID = 1;
public enum Mode {LAUNCH, VERIFY, RESULT}
@@ -155,10 +156,16 @@ public abstract class BaseActivity extends AppCompatActivity implements
/**
* Launches activity with the selected options.
*/
public void launchActivity(Intent intent) {
@Override
public void launchActivity(Intent intent, boolean forResult) {
if (forResult) {
startActivityForResult(intent, LAUNCH_FOR_RESULT_ID);
} else {
startActivity(intent);
}
// If people press back we want them to see the overview rather than the launch fragment.
// To achieve this we pop the launchFragment from the stack when we go to the next activity.
startActivity(intent);
getSupportFragmentManager().popBackStack();
}

View File

@@ -215,6 +215,11 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
}
public boolean startForResult() {
RadioButton startNormal = mLayout.findViewById(R.id.start_normal);
return !startNormal.isChecked();
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
int buttonId = compoundButton.getId();
@@ -358,6 +363,6 @@ public class IntentBuilderView extends FrameLayout implements View.OnClickListen
* button within this view.
*/
public interface OnLaunchCallback {
void launchActivity(Intent intent);
void launchActivity(Intent intent, boolean forResult);
}
}

View File

@@ -16,6 +16,7 @@
package com.example.android.intentplayground;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
@@ -74,7 +75,9 @@ public class LaunchFragment extends Fragment {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.app_bar_launch && mOnLaunchCallback != null) {
mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent());
Intent intent = mIntentBuilderView.currentIntent();
boolean forResult = mIntentBuilderView.startForResult();
mOnLaunchCallback.launchActivity(mIntentBuilderView.currentIntent(), forResult);
}
return super.onOptionsItemSelected(item);

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.intentplayground;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* A singleInstance activity that is responsible for a launching a bootstrap stack of activities.
*/
public class LauncherActivity extends BaseActivity {
public static final String TAG = "LauncherActivity";
private static final long SNACKBAR_DELAY = 75;
private TestBase mTester;
private boolean mFirstLaunch;
private View mSnackBarRootView;
private boolean mSnackBarIsVisible = false;
private boolean mDontLaunch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupTaskPreset();
mFirstLaunch = true;
}
/**
* Launches activity with the selected options.
*/
@Override
public void launchActivity(Intent intent) {
startActivityForResult(intent, LAUNCH_REQUEST_CODE);
// If people press back we want them to see the overview rather than the launch fragment.
// To achieve this we pop the launchFragment from the stack when we go to the next activity.
getSupportFragmentManager().popBackStack();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
}

View File

@@ -80,7 +80,7 @@ public class TestBase {
tsb.startActivities();
Thread.sleep(500);
} catch (InterruptedException ie) {
Log.e(LauncherActivity.TAG, ie.getMessage());
Log.e(TAG, ie.getMessage());
}
});
break;
@@ -91,7 +91,7 @@ public class TestBase {
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
Log.e(LauncherActivity.TAG, ie.getMessage());
Log.e(TAG, ie.getMessage());
}
ArrayList<Intent> nextIntents = new ArrayList<>(Arrays.asList(
tsb.getIntents()));
@@ -348,16 +348,6 @@ public class TestBase {
}
}
public static void clearRunningTasks(Context context) {
ComponentName launcher = new ComponentName(context, LauncherActivity.class);
context.getSystemService(ActivityManager.class).getAppTasks().stream()
.filter(task -> {
ActivityManager.RecentTaskInfo info = task.getTaskInfo();
return (info.baseActivity != null) && (!info.baseActivity.equals(launcher));
})
.forEach(ActivityManager.AppTask::finishAndRemoveTask);
}
public Context getContext() { return mContext; }
/**