@Override
 protected void onDestroy() {
   mEnhancedBookmarksModel.removeObserver(mBookmarkModelObserver);
   mEnhancedBookmarksModel.destroy();
   mEnhancedBookmarksModel = null;
   super.onDestroy();
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    EnhancedBookmarkUtils.setTaskDescriptionInDocumentMode(this, getString(R.string.edit_bookmark));
    mEnhancedBookmarksModel = new EnhancedBookmarksModel();
    mBookmarkId =
        BookmarkId.getBookmarkIdFromString(getIntent().getStringExtra(INTENT_BOOKMARK_ID));
    mEnhancedBookmarksModel.addObserver(mBookmarkModelObserver);

    setContentView(R.layout.eb_edit);
    mTitleEditText = (EmptyAlertEditText) findViewById(R.id.title_text);
    mUrlEditText = (EmptyAlertEditText) findViewById(R.id.url_text);
    mFolderTextView = (TextView) findViewById(R.id.folder_text);
    mFolderTextView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            EnhancedBookmarkFolderSelectActivity.startFolderSelectActivity(
                EnhancedBookmarkEditActivity.this, mBookmarkId);
          }
        });
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    updateViewContent();
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int title =
        OfflinePageBridge.isEnabled() ? R.string.offline_pages_edit_item : R.string.edit_bookmark;
    setTitle(title);
    EnhancedBookmarkUtils.setTaskDescriptionInDocumentMode(this, getString(title));
    mEnhancedBookmarksModel = new EnhancedBookmarksModel();
    mBookmarkId =
        BookmarkId.getBookmarkIdFromString(getIntent().getStringExtra(INTENT_BOOKMARK_ID));
    mEnhancedBookmarksModel.addObserver(mBookmarkModelObserver);
    assert mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).isEditable();

    setContentView(R.layout.eb_edit);
    mTitleEditText = (EmptyAlertEditText) findViewById(R.id.title_text);
    mFolderTextView = (TextView) findViewById(R.id.folder_text);
    mUrlEditText = (EmptyAlertEditText) findViewById(R.id.url_text);

    mFolderTextView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            EnhancedBookmarkFolderSelectActivity.startFolderSelectActivity(
                EnhancedBookmarkEditActivity.this, mBookmarkId);
          }
        });

    if (OfflinePageBridge.isEnabled()
        && OfflinePageBridge.canSavePage(
            mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).getUrl())) {
      mOfflinePageModelObserver =
          new OfflinePageModelObserver() {
            @Override
            public void offlinePageDeleted(BookmarkId bookmarkId) {
              if (mBookmarkId.equals(bookmarkId)) {
                updateOfflineSection();
              }
            }
          };

      mEnhancedBookmarksModel.getOfflinePageBridge().addObserver(mOfflinePageModelObserver);
      // Make offline page section visible and find controls.
      findViewById(R.id.offline_page_group).setVisibility(View.VISIBLE);
      getIntent().setExtrasClassLoader(WebContents.class.getClassLoader());
      mWebContents = getIntent().getParcelableExtra(INTENT_WEB_CONTENTS);
      mActivityWindowAndroid = new ActivityWindowAndroid(this, false);
      updateOfflineSection();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    updateViewContent();
  }
 @Override
 protected void onDestroy() {
   recordOfflineButtonAction(false);
   if (OfflinePageBridge.isEnabled()) {
     mEnhancedBookmarksModel.getOfflinePageBridge().removeObserver(mOfflinePageModelObserver);
   }
   mEnhancedBookmarksModel.removeObserver(mBookmarkModelObserver);
   mEnhancedBookmarksModel.destroy();
   mEnhancedBookmarksModel = null;
   super.onDestroy();
 }
  @Override
  public void onRequestPermissionsResult(
      int requestCode, String[] permissions, int[] grantResults) {
    if (mActivityWindowAndroid != null) {
      if (mActivityWindowAndroid.onRequestPermissionsResult(
          requestCode, permissions, grantResults)) {
        return;
      }
    }

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    }
    mSnackbarManager = new SnackbarManager(getWindow());
    mBookmarkManager = new EnhancedBookmarkManager(this);
    setContentView(mBookmarkManager.getView());
    EnhancedBookmarkUtils.setTaskDescriptionInDocumentMode(this, getString(R.string.bookmarks));

    // Hack to work around inferred theme false lint error: http://crbug.com/445633
    assert (R.layout.eb_main_content != 0);
  }
  @Override
  public void onBackPressed() {
    String newTitle = mTitleEditText.getTrimmedText();
    String newUrl = mUrlEditText.getTrimmedText();
    newUrl = UrlUtilities.fixupUrl(newUrl);
    if (newUrl == null) newUrl = "";
    mUrlEditText.setText(newUrl);

    if (!mTitleEditText.validate() || !mUrlEditText.validate()) return;

    mEnhancedBookmarksModel.setBookmarkTitle(mBookmarkId, newTitle);
    mEnhancedBookmarksModel.setBookmarkUrl(mBookmarkId, newUrl);
    super.onBackPressed();
  }
  @Override
  protected void onStop() {
    if (mEnhancedBookmarksModel.doesBookmarkExist(mBookmarkId)) {
      final String title = mTitleEditText.getTrimmedText();
      final String url = mUrlEditText.getTrimmedText();

      if (!mTitleEditText.isEmpty()) {
        mEnhancedBookmarksModel.setBookmarkTitle(mBookmarkId, title);
      }

      if (!mUrlEditText.isEmpty()
          && mEnhancedBookmarksModel.getBookmarkById(mBookmarkId).isUrlEditable()) {
        String fixedUrl = UrlUtilities.fixupUrl(url);
        if (fixedUrl != null) mEnhancedBookmarksModel.setBookmarkUrl(mBookmarkId, fixedUrl);
      }
    }

    super.onStop();
  }
 @Override
 public void onBackPressed() {
   if (!mBookmarkManager.onBackPressed()) super.onBackPressed();
 }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   mBookmarkManager.destroy();
   mSnackbarManager.dismissSnackbar(false);
 }