android.arch.lifecycle.LifecycleObserver Java Examples

The following examples show how to use android.arch.lifecycle.LifecycleObserver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NavigationScene.java    From scene with Apache License 2.0 6 votes vote down vote up
@MainThread
public void addNavigationListener(@NonNull final LifecycleOwner lifecycleOwner, @NonNull final NavigationListener listener) {
    ThreadUtility.checkUIThread();
    if (lifecycleOwner.getLifecycle().getCurrentState() == DESTROYED) {
        // ignore
        return;
    }
    this.mNavigationListenerList.add(listener);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mNavigationListenerList.remove(listener);
        }
    });
}
 
Example #2
Source File: NavigationScene.java    From scene with Apache License 2.0 6 votes vote down vote up
@MainThread
public void addOnBackPressedListener(@NonNull final LifecycleOwner lifecycleOwner, @NonNull final OnBackPressedListener onBackPressedListener) {
    ThreadUtility.checkUIThread();
    if (lifecycleOwner.getLifecycle().getCurrentState() == DESTROYED) {
        // ignore
        return;
    }
    this.mNavigationSceneManager.addOnBackPressedListener(lifecycleOwner, onBackPressedListener);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mNavigationSceneManager.removeOnBackPressedListener(onBackPressedListener);
        }
    });
}
 
Example #3
Source File: SceneActivityCompatibilityLayerFragment.java    From scene with Apache License 2.0 6 votes vote down vote up
@MainThread
void startActivityForResultByScene(@NonNull final LifecycleOwner lifecycleOwner, @NonNull Intent intent,
                                   final int requestCode, @NonNull ActivityResultCallback resultCallback) {
    if (!isCurrentStatusValid(lifecycleOwner)) {
        return;
    }

    if (requestCode < 0) {
        startActivity(intent);
        return;
    }
    mResultCallbackMap.put(requestCode, resultCallback);
    startActivityForResult(intent, requestCode);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mResultCallbackMap.remove(requestCode);
        }
    });
}
 
Example #4
Source File: SceneActivityCompatibilityLayerFragment.java    From scene with Apache License 2.0 6 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@MainThread
void startActivityForResultByScene(@NonNull final LifecycleOwner lifecycleOwner, @NonNull Intent intent,
                                   final int requestCode, @Nullable Bundle options,
                                   @NonNull ActivityResultCallback resultCallback) {
    if (!isCurrentStatusValid(lifecycleOwner)) {
        return;
    }

    if (requestCode < 0) {
        startActivity(intent);
        return;
    }
    mResultCallbackMap.put(requestCode, resultCallback);
    startActivityForResult(intent, requestCode, options);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mResultCallbackMap.remove(requestCode);
        }
    });
}
 
Example #5
Source File: SceneActivityCompatibilityLayerFragment.java    From scene with Apache License 2.0 6 votes vote down vote up
@MainThread
@RequiresApi(Build.VERSION_CODES.M)
void requestPermissionsByScene(@NonNull final LifecycleOwner lifecycleOwner, @NonNull String[] permissions,
                               final int requestCode, @NonNull final PermissionResultCallback resultCallback) {
    if (!isCurrentStatusValid(lifecycleOwner)) {
        return;
    }
    mPermissionResultCallbackMap.put(requestCode, resultCallback);
    requestPermissions(permissions, requestCode);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mPermissionResultCallbackMap.remove(requestCode);
        }
    });
}
 
Example #6
Source File: SceneActivityCompatibilityLayerFragment.java    From scene with Apache License 2.0 5 votes vote down vote up
@MainThread
void addConfigurationChangedListener(@NonNull final LifecycleOwner lifecycleOwner,
                                     @NonNull final ConfigurationChangedListener configurationChangedListener) {
    if (!isCurrentStatusValid(lifecycleOwner)) {
        return;
    }
    mConfigurationChangedListenerList.add(configurationChangedListener);
    lifecycleOwner.getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        void onDestroy() {
            lifecycleOwner.getLifecycle().removeObserver(this);
            mConfigurationChangedListenerList.remove(configurationChangedListener);
        }
    });
}
 
Example #7
Source File: HomeActivity.java    From alpha-wallet-android with MIT License 5 votes vote down vote up
public HomeActivity()
{
    importFileName = null;
    if (VisibilityFilter.hideDappBrowser()) dappBrowserFragment = new Fragment();
    else dappBrowserFragment = new DappBrowserFragment();
    transactionsFragment = new TransactionsFragment();
    settingsFragment = new NewSettingsFragment();
    walletFragment = new WalletFragment();
    lifeCycle = new LifecycleObserver()
    {
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        private void onMoveToForeground()
        {
            Log.d("LIFE", "AlphaWallet into foreground");
            ((WalletFragment)walletFragment).walletInFocus();
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        private void onMoveToBackground()
        {
            Log.d("LIFE", "AlphaWallet into background");
            ((WalletFragment)walletFragment).walletOutOfFocus();
        }

        @Override
        public int hashCode()
        {
            return super.hashCode();
        }
    };

    ProcessLifecycleOwner.get().getLifecycle().addObserver(lifeCycle);
}
 
Example #8
Source File: Flutter.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a {@link FlutterView} linked to the specified {@link Activity} and {@link Lifecycle}.
 * The optional initial route string will be made available to the Dart code (via
 * {@code window.defaultRouteName}) and may be used to determine which widget should be displayed
 * in the view. The default initialRoute is "/".
 *
 * @param activity an {@link Activity}
 * @param lifecycle a {@link Lifecycle}
 * @param initialRoute an initial route {@link String}, or null
 * @return a {@link FlutterView}
 */
@NonNull
public static FlutterView createView(@NonNull final Activity activity, @NonNull final Lifecycle lifecycle, final String initialRoute) {
  FlutterMain.startInitialization(activity.getApplicationContext());
  FlutterMain.ensureInitializationComplete(activity.getApplicationContext(), null);
  final FlutterNativeView nativeView = new FlutterNativeView(activity);
  final FlutterView flutterView = new FlutterView(activity, null, nativeView) {
    private final BasicMessageChannel<String> lifecycleMessages = new BasicMessageChannel<>(this, "flutter/lifecycle", StringCodec.INSTANCE);
    @Override
    public void onFirstFrame() {
      super.onFirstFrame();
      setAlpha(1.0f);
    }

    @Override
    public void onPostResume() {
      // Overriding default behavior to avoid dictating system UI via PlatformPlugin.
      lifecycleMessages.send("AppLifecycleState.resumed");
    }
  };
  if (initialRoute != null) {
    flutterView.setInitialRoute(initialRoute);
  }
  lifecycle.addObserver(new LifecycleObserver() {
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
      final FlutterRunArguments arguments = new FlutterRunArguments();
      arguments.bundlePath = FlutterMain.findAppBundlePath(activity.getApplicationContext());
      arguments.entrypoint = "main";
      flutterView.runFromBundle(arguments);
      GeneratedPluginRegistrant.registerWith(flutterView.getPluginRegistry());
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
      flutterView.onStart();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
      flutterView.onPostResume();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
      flutterView.onPause();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
      flutterView.onStop();
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
      flutterView.destroy();
    }
  });
  flutterView.setAlpha(0.0f);
  return flutterView;
}
 
Example #9
Source File: BasePresenter.java    From Aurora with Apache License 2.0 5 votes vote down vote up
@Override
public void onStart() {
    //将 LifecycleObserver 注册给 LifecycleOwner 后 @OnLifecycleEvent 才可以正常使用
    if (mRootView != null && mRootView instanceof LifecycleOwner) {
        ((LifecycleOwner) mRootView).getLifecycle().addObserver(this);
        if (mModel!= null && mModel instanceof LifecycleObserver){
            ((LifecycleOwner) mRootView).getLifecycle().addObserver((LifecycleObserver) mModel);
        }
    }
    if (useEventBus())//如果要使用 Eventbus 请将此方法返回 true
        EventBus.getDefault().register(this);//注册 Eventbus
}
 
Example #10
Source File: BaseActivity.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //设置DataBinding
    mBinding = DataBindingUtil.setContentView(this, initView(savedInstanceState));
    initData(savedInstanceState);
    if (mViewModel != null) {
        getLifecycle().addObserver((LifecycleObserver) mViewModel);
    }
}
 
Example #11
Source File: BaseActivity.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDestroy() {
    super.onDestroy();
    this.mBinding = null;
    this.mViewModelFactory = null;
    //移除LifecycleObserver
    if (mViewModel != null) {
        getLifecycle().removeObserver((LifecycleObserver) mViewModel);
    }
    this.mViewModel = null;
}
 
Example #12
Source File: BaseFragment.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    mRootView = initView(inflater, container, savedInstanceState);
    if (mViewModel != null) {
        getLifecycle().addObserver((LifecycleObserver) mViewModel);
    }
    //可见,并且是首次加载
    if (mVisible && mFirst) {
        onFragmentVisibleChange(true);
    }
    return mRootView;
}
 
Example #13
Source File: BaseFragment.java    From MVVMArms with Apache License 2.0 5 votes vote down vote up
@Override
public void onDestroy() {
    super.onDestroy();
    this.mBinding = null;
    this.mRootView = null;
    this.mViewModelFactory = null;
    //移除LifecycleObserver
    if (mViewModel != null) {
        getLifecycle().removeObserver((LifecycleObserver) mViewModel);
    }
    this.mViewModel = null;
}
 
Example #14
Source File: UserVisibleHintGroupScene.java    From scene with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);

    getLifecycle().addObserver(new LifecycleObserver() {
        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        void onPause() {
            mResume = false;
            if (mUserVisibleHint) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE);
            }
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        void onResume() {
            mResume = true;
            if (mUserVisibleHint) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
            }
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        void onStop() {
            mStart = false;
            if (mUserVisibleHint) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
            }
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        void onStart() {
            mStart = true;
            if (mUserVisibleHint) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
            }
        }

        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        void onDestroy() {
            mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        }
    });
}
 
Example #15
Source File: TypedTextView.java    From TypedTextView with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a LifecycleObserver that expects to be notified when the LifecycleOwner changes state.
 * Add this as a {@link LifecycleObserver} to {@link android.support.v7.app.AppCompatActivity} or
 * {@link android.support.v4.app.Fragment}
 *
 * @return LifecycleObserver
 */
public LifecycleObserver getLifecycleObserver()
{
    return this;
}