Add lifecycle methods for managing the options menu.
Test: Settings robotests Change-Id: I087cd0ec097041a4c4e0dd561dd2ec62f45f5390
This commit is contained in:
@@ -18,12 +18,19 @@ package com.android.settings.core.lifecycle;
|
||||
import android.annotation.UiThread;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.android.settings.core.lifecycle.events.OnAttach;
|
||||
import com.android.settings.core.lifecycle.events.OnCreate;
|
||||
import com.android.settings.core.lifecycle.events.OnCreateOptionsMenu;
|
||||
import com.android.settings.core.lifecycle.events.OnDestroy;
|
||||
import com.android.settings.core.lifecycle.events.OnOptionsItemSelected;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnPrepareOptionsMenu;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
import com.android.settings.core.lifecycle.events.OnSaveInstanceState;
|
||||
import com.android.settings.core.lifecycle.events.OnStart;
|
||||
@@ -122,4 +129,31 @@ public class Lifecycle {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onCreateOptionsMenu(final Menu menu, final @Nullable MenuInflater inflater) {
|
||||
for (LifecycleObserver observer : mObservers) {
|
||||
if (observer instanceof OnCreateOptionsMenu) {
|
||||
((OnCreateOptionsMenu) observer).onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onPrepareOptionsMenu(final Menu menu) {
|
||||
for (LifecycleObserver observer : mObservers) {
|
||||
if (observer instanceof OnPrepareOptionsMenu) {
|
||||
((OnPrepareOptionsMenu) observer).onPrepareOptionsMenu(menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean onOptionsItemSelected(final MenuItem menuItem) {
|
||||
for (LifecycleObserver observer : mObservers) {
|
||||
if (observer instanceof OnOptionsItemSelected) {
|
||||
if (((OnOptionsItemSelected) observer).onOptionsItemSelected(menuItem)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@@ -19,6 +19,8 @@ import android.annotation.Nullable;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
/**
|
||||
* {@link Activity} that has hooks to observe activity lifecycle events.
|
||||
@@ -73,4 +75,31 @@ public class ObservableActivity extends Activity {
|
||||
mLifecycle.onDestroy();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(final Menu menu) {
|
||||
if (super.onCreateOptionsMenu(menu)) {
|
||||
mLifecycle.onCreateOptionsMenu(menu, null);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(final Menu menu) {
|
||||
if (super.onPrepareOptionsMenu(menu)) {
|
||||
mLifecycle.onPrepareOptionsMenu(menu);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem menuItem) {
|
||||
boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
|
||||
if (!lifecycleHandled) {
|
||||
return super.onOptionsItemSelected(menuItem);
|
||||
}
|
||||
return lifecycleHandled;
|
||||
}
|
||||
}
|
||||
|
@@ -17,6 +17,9 @@ package com.android.settings.core.lifecycle;
|
||||
|
||||
import android.app.DialogFragment;
|
||||
import android.content.Context;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
/**
|
||||
* {@link DialogFragment} that has hooks to observe fragment lifecycle events.
|
||||
@@ -61,4 +64,24 @@ public class ObservableDialogFragment extends DialogFragment {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
|
||||
mLifecycle.onCreateOptionsMenu(menu, inflater);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(final Menu menu) {
|
||||
mLifecycle.onPrepareOptionsMenu(menu);
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem menuItem) {
|
||||
boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
|
||||
if (!lifecycleHandled) {
|
||||
return super.onOptionsItemSelected(menuItem);
|
||||
}
|
||||
return lifecycleHandled;
|
||||
}
|
||||
}
|
||||
|
@@ -20,6 +20,9 @@ import android.annotation.CallSuper;
|
||||
import android.app.Fragment;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
public class ObservableFragment extends Fragment {
|
||||
|
||||
@@ -84,4 +87,28 @@ public class ObservableFragment extends Fragment {
|
||||
mLifecycle.onDestroy();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
|
||||
mLifecycle.onCreateOptionsMenu(menu, inflater);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(final Menu menu) {
|
||||
mLifecycle.onPrepareOptionsMenu(menu);
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem menuItem) {
|
||||
boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
|
||||
if (!lifecycleHandled) {
|
||||
return super.onOptionsItemSelected(menuItem);
|
||||
}
|
||||
return lifecycleHandled;
|
||||
}
|
||||
}
|
@@ -21,6 +21,9 @@ import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v7.preference.PreferenceScreen;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
/**
|
||||
* {@link PreferenceFragment} that has hooks to observe fragment lifecycle events.
|
||||
@@ -95,4 +98,27 @@ public abstract class ObservablePreferenceFragment extends PreferenceFragment {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
|
||||
mLifecycle.onCreateOptionsMenu(menu, inflater);
|
||||
super.onCreateOptionsMenu(menu, inflater);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public void onPrepareOptionsMenu(final Menu menu) {
|
||||
mLifecycle.onPrepareOptionsMenu(menu);
|
||||
super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@CallSuper
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(final MenuItem menuItem) {
|
||||
boolean lifecycleHandled = mLifecycle.onOptionsItemSelected(menuItem);
|
||||
if (!lifecycleHandled) {
|
||||
return super.onOptionsItemSelected(menuItem);
|
||||
}
|
||||
return lifecycleHandled;
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.android.settings.core.lifecycle.events;
|
||||
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
|
||||
public interface OnCreateOptionsMenu {
|
||||
void onCreateOptionsMenu(Menu menu, MenuInflater inflater);
|
||||
}
|
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.android.settings.core.lifecycle.events;
|
||||
|
||||
import android.view.MenuItem;
|
||||
|
||||
public interface OnOptionsItemSelected {
|
||||
boolean onOptionsItemSelected(MenuItem menuItem);
|
||||
}
|
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.android.settings.core.lifecycle.events;
|
||||
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
|
||||
public interface OnPrepareOptionsMenu {
|
||||
void onPrepareOptionsMenu(Menu menu);
|
||||
}
|
Reference in New Issue
Block a user