Java Code Examples for androidx.core.app.NavUtils#getParentActivityIntent()

The following examples show how to use androidx.core.app.NavUtils#getParentActivityIntent() . 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: FeedActivity.java    From android-mvvm-architecture with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            upIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                // This activity is NOT part of this app's task, so create a new task
                // when navigating up, with a synthesized back stack.
                TaskStackBuilder.create(this)
                        // Add all of this activity's parents to the back stack
                        .addNextIntentWithParentStack(upIntent)
                        // Navigate up to the closest parent
                        .startActivities();
            } else {
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            }
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 2
Source File: EditTriggerFragment.java    From science-journal with Apache License 2.0 6 votes vote down vote up
private void goToParent() {
  if (getActivity() == null) {
    return;
  }
  // To restart the TriggerListActivity, need to pass back the trigger and layout info.
  Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
  upIntent.putExtra(TriggerListActivity.EXTRA_ACCOUNT_KEY, appAccount.getAccountKey());
  upIntent.putExtra(TriggerListActivity.EXTRA_SENSOR_ID, sensorId);
  upIntent.putExtra(TriggerListActivity.EXTRA_EXPERIMENT_ID, experimentId);
  upIntent.putExtra(TriggerListActivity.EXTRA_LAYOUT_POSITION, sensorLayoutPosition);
  upIntent.putExtra(
      TriggerListActivity.EXTRA_TRIGGER_ORDER,
      getArguments().getStringArrayList(TriggerListActivity.EXTRA_TRIGGER_ORDER));
  if (getActivity() != null) {
    NavUtils.navigateUpTo(getActivity(), upIntent);
  } else {
    Log.e(TAG, "Can't exit activity because it's no longer there.");
  }
}
 
Example 3
Source File: AppUtils.java    From MaterialProgressBar with Apache License 2.0 6 votes vote down vote up
public static void navigateUp(Activity activity, Bundle extras) {
    Intent upIntent = NavUtils.getParentActivityIntent(activity);
    if (upIntent != null) {
        if (extras != null) {
            upIntent.putExtras(extras);
        }
        if (NavUtils.shouldUpRecreateTask(activity, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(activity)
                    // Add all of this activity's parents to the back stack.
                    .addNextIntentWithParentStack(upIntent)
                            // Navigate up to the closest parent.
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.
            // According to http://stackoverflow.com/a/14792752/2420519
            //NavUtils.navigateUpTo(activity, upIntent);
            upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            activity.startActivity(upIntent);
        }
    }
    activity.finish();
}
 
Example 4
Source File: DetailFragment.java    From wear-os-samples with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // Some small additions to handle "up" navigation correctly
            Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
            upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

            // Check if up activity needs to be created (usually when
            // detail screen is opened from a notification or from the
            // Wearable app
            if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)
                    || getActivity().isTaskRoot()) {

                // Synthesize parent stack
                TaskStackBuilder.create(getActivity())
                        .addNextIntentWithParentStack(upIntent)
                        .startActivities();
            }

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                // On Lollipop+ we finish so to run the nice animation
                getActivity().finishAfterTransition();
                return true;
            }

            // Otherwise let the system handle navigating "up"
            return false;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 5
Source File: ExperimentDetailsFragment.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void goToExperimentList() {
  if (getActivity() == null) {
    if (Log.isLoggable(TAG, Log.DEBUG)) {
      Log.d(TAG, "goToExperimentList called on null activity");
    }
    return;
  }

  if (claimExperimentsMode) {
    getActivity().finish();
    return;
  }

  Intent upIntent =
      getActivity() == null ? null : NavUtils.getParentActivityIntent(getActivity());
  if (upIntent == null) {
    // This is cheating a bit.  Currently, upIntent has only been observed to be null
    // when we're using panes mode, so here we just assume usePanes==true.
    Intent intent =
        MainActivity.launchIntent(getActivity(), R.id.navigation_item_experiments, true);
    getActivity().startActivity(intent);
    getActivity().finish();
    return;
  }
  if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)
      || getArguments().getBoolean(ARG_CREATE_TASK, false)) {
    upIntent.putExtra(MainActivity.ARG_SELECTED_NAV_ITEM_ID, R.id.navigation_item_experiments);
    upIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // TODO: Transition animation
    TaskStackBuilder.create(getActivity())
        .addNextIntentWithParentStack(upIntent)
        .startActivities();
  } else {
    NavUtils.navigateUpTo(getActivity(), upIntent);
  }
}
 
Example 6
Source File: UpdateRunFragment.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void returnToRunReview() {
  Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
  upIntent.putExtra(RunReviewActivity.EXTRA_FROM_RECORD, false);
  upIntent.putExtra(RunReviewFragment.ARG_ACCOUNT_KEY, appAccount.getAccountKey());
  upIntent.putExtra(RunReviewFragment.ARG_START_LABEL_ID, runId);
  upIntent.putExtra(RunReviewFragment.ARG_EXPERIMENT_ID, experimentId);
  upIntent.putExtra(RunReviewFragment.ARG_CLAIM_EXPERIMENTS_MODE, false);
  NavUtils.navigateUpTo(getActivity(), upIntent);
}
 
Example 7
Source File: ApplozicSetting.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Intent getParentActivityIntent(Activity sourceActivity) {
    String parentActivityName = sharedPreferences.getString(PARENT_ACTIVITY_INTENT, null);

    try {
        if (parentActivityName != null) {
            final ComponentName target = new ComponentName(sourceActivity, parentActivityName);
            return new Intent().setComponent(target);
        } else {
            return NavUtils.getParentActivityIntent(sourceActivity);
        }
    } catch (Exception e) {
        e.printStackTrace();
        return NavUtils.getParentActivityIntent(sourceActivity);
    }
}
 
Example 8
Source File: VideoActivity.java    From evercam-android with GNU Affero General Public License v3.0 5 votes vote down vote up
/************************
 * Private Methods
 ************************/

private void navigateBackToCameraList() {
    if (CamerasActivity.activity == null) {
        if (android.os.Build.VERSION.SDK_INT >= 16) {
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            TaskStackBuilder.create(this)
                    .addNextIntentWithParentStack(upIntent)
                    .startActivities();
        }
    }

    finish();
}
 
Example 9
Source File: RunReviewFragment.java    From science-journal with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();

  //noinspection SimplifiableIfStatement
  if (id == android.R.id.home) {
    Intent upIntent = NavUtils.getParentActivityIntent(getActivity());
    if (experiment != null) {
      String accountKey = appAccount.getAccountKey();
      // Ensure that we set the values we need to go up to ExperimentActivity from RunReview after
      // starting the app from a notification (b/66162829).
      upIntent.putExtra(ExperimentActivity.EXTRA_ACCOUNT_KEY, accountKey);
      upIntent.putExtra(ExperimentActivity.EXTRA_EXPERIMENT_ID, experimentId);
      upIntent.putExtra(ExperimentActivity.EXTRA_CLAIM_EXPERIMENTS_MODE, claimExperimentsMode);

      upIntent.putExtra(ExperimentDetailsFragment.ARG_ACCOUNT_KEY, accountKey);
      upIntent.putExtra(ExperimentDetailsFragment.ARG_EXPERIMENT_ID, experimentId);
      upIntent.putExtra(
              ExperimentDetailsFragment.ARG_CREATE_TASK,
          getArguments().getBoolean(ARG_CREATE_TASK, false));
      upIntent.putExtra(
              ExperimentDetailsFragment.ARG_CLAIM_EXPERIMENTS_MODE, claimExperimentsMode);
      upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
      getActivity().startActivity(upIntent, null);
    } else if (getActivity() != null) {
      // This is a weird error situation: we didn't load the experiment run at all.
      // In this case, just finish.
      getActivity().onBackPressed();
      return true;
    }
    return true;
  } else if (id == R.id.action_graph_options) {
    graphOptionsController.launchOptionsDialog(
        scalarDisplayOptions, new NewOptionsStorage.SnackbarFailureListener(getView()));
  } else if (id == R.id.action_export) {
    RxDataController.getExperimentById(getDataController(), experimentId)
        .subscribe(
            experiment -> exportRun(experiment.getTrial(trialId), false),
            error -> {
              if (Log.isLoggable(TAG, Log.ERROR)) {
                Log.e(TAG, "Export in RunReviewFragment failed", error);
              }
              throw new IllegalStateException("Export in RunReviewFragment failed", error);
            });
  } else if (id == R.id.action_download) {
    RxDataController.getExperimentById(getDataController(), experimentId)
        .subscribe(
            experiment -> exportRun(experiment.getTrial(trialId), true),
            error -> {
              if (Log.isLoggable(TAG, Log.ERROR)) {
                Log.e(TAG, "Download in RunReviewFragment failed", error);
              }
              throw new IllegalStateException("Download in RunReviewFragment failed", error);
            });
  } else if (id == R.id.action_run_review_crop) {
    if (experiment != null) {
      launchCrop(getView());
    }
  } else if (id == R.id.action_run_review_delete) {
    if (experiment != null) {
      deleteThisRun();
    }
  } else if (id == R.id.action_run_review_archive) {
    if (experiment != null) {
      setArchived(true);
    }
  } else if (id == R.id.action_run_review_unarchive) {
    if (experiment != null) {
      setArchived(false);
    }
  } else if (id == R.id.action_run_review_edit) {
    UpdateRunActivity.launch(getActivity(), appAccount, trialId, experimentId);
  } else if (id == R.id.action_enable_auto_zoom) {
    if (experiment != null) {
      setAutoZoomEnabled(true);
    }
  } else if (id == R.id.action_disable_auto_zoom) {
    if (experiment != null) {
      setAutoZoomEnabled(false);
    }
  } else if (id == R.id.action_run_review_audio_settings) {
    launchAudioSettings();
  }
  return super.onOptionsItemSelected(item);
}
 
Example 10
Source File: AppCompatPreferenceActivity.java    From MHViewer with Apache License 2.0 2 votes vote down vote up
/**
 * Obtain an {@link android.content.Intent} that will launch an explicit target activity
 * specified by sourceActivity's {@link androidx.core.app.NavUtils#PARENT_ACTIVITY} <meta-data>
 * element in the application's manifest. If the device is running
 * Jellybean or newer, the android:parentActivityName attribute will be preferred
 * if it is present.
 *
 * @return a new Intent targeting the defined parent activity of sourceActivity
 */
@Nullable
@Override
public Intent getSupportParentActivityIntent() {
    return NavUtils.getParentActivityIntent(this);
}
 
Example 11
Source File: AppCompatPreferenceActivity.java    From EhViewer with Apache License 2.0 2 votes vote down vote up
/**
 * Obtain an {@link android.content.Intent} that will launch an explicit target activity
 * specified by sourceActivity's {@link androidx.core.app.NavUtils#PARENT_ACTIVITY} <meta-data>
 * element in the application's manifest. If the device is running
 * Jellybean or newer, the android:parentActivityName attribute will be preferred
 * if it is present.
 *
 * @return a new Intent targeting the defined parent activity of sourceActivity
 */
@Nullable
@Override
public Intent getSupportParentActivityIntent() {
    return NavUtils.getParentActivityIntent(this);
}