Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.support.appnavigation.app;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.os.Bundle;
     22 import android.view.MenuItem;
     23 import android.widget.TextView;
     24 
     25 import androidx.core.app.NavUtils;
     26 import androidx.core.app.TaskStackBuilder;
     27 
     28 import com.example.android.support.appnavigation.R;
     29 
     30 public class ContentViewActivity extends Activity {
     31     public static final String EXTRA_TEXT = "com.example.android.appnavigation.EXTRA_TEXT";
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.content_view);
     37 
     38         ActionBarCompat.setDisplayHomeAsUpEnabled(this, true);
     39 
     40         Intent intent = getIntent();
     41         if (Intent.ACTION_VIEW.equals(intent.getAction())) {
     42             TextView tv = findViewById(R.id.status_text);
     43             tv.setText("Viewing content from ACTION_VIEW");
     44         } else if (intent.hasExtra(EXTRA_TEXT)) {
     45             TextView tv = findViewById(R.id.status_text);
     46             tv.setText(intent.getStringExtra(EXTRA_TEXT));
     47         }
     48     }
     49 
     50     @Override
     51     public boolean onOptionsItemSelected(MenuItem item) {
     52         if (item.getItemId() == android.R.id.home) {
     53             Intent upIntent = NavUtils.getParentActivityIntent(this);
     54             if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
     55                 TaskStackBuilder.from(this)
     56                         .addParentStack(this)
     57                         .startActivities();
     58                 finish();
     59             } else {
     60                 NavUtils.navigateUpTo(this, upIntent);
     61             }
     62             return true;
     63         }
     64         return super.onOptionsItemSelected(item);
     65     }
     66 }
     67