Android Action button for navigation up does not work on devices

Action bar on the action bar Action Bar Back Button works fine in the emulator, but doesn't work in the device, can someone help me

Mainactivity

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mainpage); tf = Typeface.createFromAsset(getAssets(), "font/AGENCYB.TTF"); Button btnWeatherDetails = (Button) findViewById(R.id.btnWeather); Button btnMovieDetails = (Button) findViewById(R.id.btnMovies); Button btnHoroscopeDetails = (Button) findViewById(R.id.btnHoroscope); btnWeatherDetails.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(MainActivity.this, WeatherActivity.class); MainActivity.this.startActivity(i); } }); btnMovieDetails.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent(MainActivity.this, MovieActivity.class); MainActivity.this.startActivity(i); } }); btnHoroscopeDetails.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent(MainActivity.this, HoroscopeActivity.class); MainActivity.this.startActivity(i); } }); } @Override protected void onDestroy() { SharedPreferences settingsPreferences = getSharedPreferences(PREF_NAME, MODE_PRIVATE); SharedPreferences.Editor settingsEditor= settingsPreferences.edit(); settingsEditor.putBoolean(MovieActivity.MOVIEUPDATED, false); settingsEditor.putBoolean(HoroscopeActivity.HOROSCOPEAVAILABLE, false); settingsEditor.commit(); super.onDestroy(); } 

Weatheractivity

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scrollmain); getActionBar().setDisplayHomeAsUpEnabled(true); settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); SavedString = settingsPreferences.getString(JSONWEATHERSTRING, "Not Found"); new JSONParse().execute(WeatherUrl); } public boolean onCreateOptionMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onOptionItemSelected(MenuItem item) { switch(item.getItemId()){ case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void onBackPressed() { moveTaskToBack(true); WeatherActivity.this.finish(); } 

Movieactivity

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); MovieJsonString = settingsPreferences.getString(JSONMOVIESTRING, "Not Found"); isMovieUpdated = settingsPreferences.getBoolean(MOVIEUPDATED, false); setContentView(R.layout.main_list_view); getActionBar().setDisplayHomeAsUpEnabled(true); movieList = new ArrayList<HashMap<String, String>>(); list = getListView(); // Calling async task to get json if(!isMovieUpdated) new GetMovieDetails().execute(); else GetMoviePreferences(); } public boolean onCreateOptionMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onOptionItemSelected(MenuItem item) { switch(item.getItemId()){ case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void onBackPressed() { moveTaskToBack(true); MovieActivity.this.finish(); } 

Horoscope activity

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.horoscope); getActionBar().setDisplayHomeAsUpEnabled(true); settingsPreferences = getSharedPreferences(MainActivity.PREF_NAME, MODE_PRIVATE); isHoroAvailable = settingsPreferences.getBoolean(HOROSCOPEAVAILABLE,false); if(!isHoroAvailable) new XMLParse().execute(); else GetHoroscopeFromPreference(); } public boolean onCreateOptionMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } public boolean onOptionItemSelected(MenuItem item) { switch(item.getItemId()){ case android.R.id.home: NavUtils.navigateUpFromSameTask(this); return true; } return super.onOptionsItemSelected(item); } public void onBackPressed() { moveTaskToBack(true); HoroscopeActivity.this.finish(); } 

Manifest file

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="in.datumdata.weather" android:installLocation="auto" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <application android:allowBackup="true" android:icon="@drawable/ic_hosurdata" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="in.datumdata.hosurdata.MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="in.datumdata.hosurdata.MovieActivity" android:label="Movie Details" android:parentActivityName="in.datumdata.hosurdata.MainActvity" android:screenOrientation="portrait" /> <activity android:name="in.datumdata.hosurdata.WeatherActivity" android:label="Weather Details" android:parentActivityName="in.datumdata.hosurdata.MainActvity" android:screenOrientation="portrait" /> <activity android:name="in.datumdata.hosurdata.SingleContactActivity" android:label="Movie Details" android:parentActivityName="in.datumdata.hosurdata.MovieActivity" android:screenOrientation="portrait" /> <activity android:name="in.datumdata.hosurdata.HoroscopeActivity" android:label="Horoscope Details" android:parentActivityName="in.datumdata.hosurdata.MainActvity" android:screenOrientation="portrait" /> <activity android:name="in.datumdata.hosurdata.SingleHoroscopeActivity" android:label="Horoscope Details" android:parentActivityName="in.datumdata.hosurdata.HoroscopeActivity" android:screenOrientation="portrait" /> </application> </manifest> 

In all of the above actions, the navigation button Action Bar Back Button works fine in the emulator, but it doesn’t work on the device, and when I press the back button on the device enter image description here , the application becomes invisible, I want to say that its launch in the background. This thing happens in all actions for any activity that I click on the button, the same thing happens

help me. thanks in advance and sorry for my bad english.

+4
android android-activity android-actionbar navigationbar
Jul 09 '14 at 6:19 06:19
source share
4 answers

Where do you do it:

 getActionBar().setDisplayHomeAsUpEnabled(true); 

Try to do this:

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
+4
Jul 09 '14 at 8:38
source share

To work with older APIs, you must use metadata tags for the Up button:

  android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="MainActivity" /> 

EDIT:

You already have parentActivityName, so add only the meta tag to the tags of the AndroidManifest.xml file. For example:

  <activity android:name="in.datumdata.hosurdata.MovieActivity" android:label="Movie Details" android:parentActivityName="in.datumdata.hosurdata.MainActvity" android:screenOrientation="portrait"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="in.datumdata.hosurdata.MainActvity" /> </activity> 
+7
Jul 09 '14 at 6:51
source share

I had problems with the support action bar, and if you use the support bar, then you should add this line to your code.

 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

And in the manifest you need to add

 <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.ParentActivity" /> 

Thus, an activity declaration in the manifest will look like this:

 <activity android:name="com.example.MyActivity" android:label="My Activity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.example.ParentActivity" /> </activity> 

You cannot add android:parentActivityName="com.example.ParentActivity" to the activity declaration in the manifest if you use an API level less than 16.

+1
Nov 11 '15 at 6:41
source share

If your activity is called from several actions and the indication of parent activity does not match your usage scenario, you can fix this by adding the following:

  getSupportActionBar().setDisplayHomeAsUpEnabled(true); (in onCreate) @Override public boolean onNavigateUp() { finish(); return true; } @Override public boolean onSupportNavigateUp() { finish(); return true; } 
0
May 21 '19 at 8:59
source share



All Articles