bits for request codes private static fi nal int INITIAL_REQUEST_CODE_VALUE = 0x00010000; private Random mRandom = new Random(); /** * Generate a random number between the initial value (00010000) inclusive, and the max * integer value. If that number is already an existing request code, generate another until * we fi nd one that is new. * * @return the number */ private int generateRandomNumber() { int number = mRandom.nextInt((Integer.MAX_VALUE - INITIAL_REQUEST_CODE_VALUE) + 1) + INITIAL_REQUEST_CODE_VALUE; while (mRcToKey.containsKey(number)) { number = mRandom.nextInt((Integer.MAX_VALUE - INITIAL_REQUEST_CODE_VALUE) + 1) + INITIAL_REQUEST_CODE_VALUE; } return number; } } Range 65536 ~ Int.MAX_VALUE h tt ps://cs.android.com/androidx/pla tf orm/frameworks/suppo rt /+/androidx-main:activity/activity/src/main/java/androidx/activity/result/ActivityResultRegistry.java
activityViewModels() Kotlin prope rt y delegate from the // fragment-ktx a rt ifact to retrieve the ViewModel in the activity scope private val viewModel: ItemViewModel by activityViewModels() // Called when the item is clicked fun onItemClicked(item: Item) { // Set a new item viewModel.selectItem(item) } } class MainActivity : AppCompatActivity() { // Using the viewModels() Kotlin prope rt y delegate from the activity-ktx // a rt ifact to retrieve the ViewModel in the activity scope private val viewModel: ItemViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewModel.selectedItem.observe(this, Observer { item -> // Pe rf orm an action with the latest item data }) } } h tt ps://developer.android.com/guide/fragments/communicate#fragments
Bundle result) { if (result == null) { // if the given result is null, remove the result mResults.remove(requestKey); return; } // Check if there is a listener waiting for a result with this key LifecycleAwareResultListener resultListener = mResultListeners.get(requestKey); // if there is and it is sta rt ed, fi re the callback if (resultListener != null && resultListener.isAtLeast(Lifecycle.State.STARTED)) { resultListener.onFragmentResult(requestKey, result); } else { // else, save the result for later mResults.put(requestKey, result); } } @SuppressLint("SyntheticAccessor") @Override public fi nal void setResultListener(@NonNull fi nal String requestKey, @NonNull fi nal LifecycleOwner lifecycleOwner, @Nullable fi nal FragmentResultListener listener) { if (listener == null) { mResultListeners.remove(requestKey); return; } fi nal Lifecycle lifecycle = lifecycleOwner.getLifecycle(); if (lifecycle.getCurrentState() == Lifecycle.State.DESTROYED) { return; } LifecycleEventObserver observer = new LifecycleEventObserver() { @Override public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) { if (event == Lifecycle.Event.ON_START) { Bundle storedResult = mResults.get(requestKey); if (storedResult != null) { listener.onFragmentResult(requestKey, storedResult); setResult(requestKey, null); } } if (event == Lifecycle.Event.ON_DESTROY) { lifecycle.removeObserver(this); mResultListeners.remove(requestKey); } } }; lifecycle.addObserver(observer); mResultListeners.put(requestKey, new LifecycleAwareResultListener(lifecycle, listener)); }
: h tt ps://developer.android.com/training/ basics/intents/result • Communicating with fragments : h tt ps://developer.android.com/guide/ fragments/communicate