I have added a PreferenceScreen in my application that include back navigation. When I click the navigation icon, it doesn't save what I make in PreferenceScreen. So, how can I handle navigation back click.
You can simply override the onBackPressed...
#Override
public void onBackPressed()
{
// code here to save the change
super.onBackPressed(); // optional depending on your needs
}
As PreferenceActivity is a subclass of Activity, it should follow the same lifecycle. Click on the link and then navigate to Indirect Subclasses or here is the direct http://developer.android.com/reference/android/preference/PreferenceActivity.html
Edits
In your case you are using the as parent activity to MainActivity, that's why it is getting re-created when you press the navigation back. remove the line from manifest.
<activity
android:name=".UserSettingActivity"
android:label="#string/app_name"
(remove this line) android:parentActivityName=".MainActivity" >
</activity>
and change the code of your settings Activity as ..
public class UserSettingActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.user_settings);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case android.R.id.home:
Log.e("onOptionsItemSelected", "home");
finish();
return false;
}
return super.onOptionsItemSelected(item);
}
Related
I just started android programming and I created 3 activities.A button in each activity will open another.I can navigate from my 2nd activity back to my first but I can't navigate from my third activity back to my second.Here's what I tried but the app crashes when I click on the back button. I guess it's trying to jump from the third activity to the second. If it helps my second activity's java file is called CountryDetails.java
public class WikiPage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
getSupportActionBar().setTitle(R.string.title_activity_wikipedia_details);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
Simply call onBackPressed() instead of NavUtils.navigateUpFromSameTask(this)
Remove NavUtils.navigateUpFromSameTask(this);
You should use onBackPressed() only.
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
case android.R.id.home:
onBackPressed();
return true;
Use onBackPressed() instead of NavUtils.navigateUpFromSameTask(this) for activity backstake manage
I'm building a pretty simple app. At it's core are 2 screens:
1) list-screen: a list of items
2) detail-screen: a detailed view of an item
I used one Activity (which extends AppCompatActivity) with an Action-Bar, a Navigation-Drawer and a main-content part (a FrameLayout).
I used 2 different fragments for the 2 screens:
When opening the app I inflate the list-fragment into the main-content part.
When an item in the list is clicked I inflate the detail-fragment into the main-content part and it all works well.
On the detail-screen I want the Action-Bar to display an up-button that goes back to the list-screen.
Considering the fact that I am using fragments, rather than separate activites, how can I achieve that?
You can enable the up button each time the Detail Fragment loads, and disable it whenever any of the other Fragments load.
First define these methods in your Activity, which you can call from the Fragments in order to show/hide the up button:
public void showUpButton() {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
public void hideUpButton() {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
You will probably want to just enable the up button in on Resume() of the detail Fragment (MainActivity is just an example, change to the name of your Activity) .....
#Override
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
if (activity != null) {
activity.showUpButton();
}
}
Then in the other Fragments:
#Override
public void onResume() {
super.onResume();
MainActivity activity = (MainActivity)getActivity();
if (activity != null) {
activity.hideUpButton();
}
}
The next thing is to make the up button actually go back. First ensure that you're adding the Fragment with the up button to the back stack, and then add this to that Fragment.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
((MainActivity)getActivity()).onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Then in the Activity, override onBackPressed() and pop from the back stack if the FragmentManager has any entries:
#Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
if (fragmentManager.getBackStackEntryCount() > 1) {
fragmentManager.popBackStackImmediate();
} else {
super.onBackPressed();
}
}
I have added an onDestroy in my Main activity
#Override
protected void onDestroy() {
if (BTAdapter.isEnabled()) {
BTAdapter.disable();
}
this.unregisterReceiver(receiver);
super.onDestroy();
}
In my Main Activity I start another activity through Navigation Drawer
if (id == R.id.nav_devices) {
intent = new Intent("com.navigationwithrssi.RSSIActivity");
startActivity(intent);
}
But when I come back to my Main Activity (using the default back button in Toolbar), the RSSIActivity automatically disables Bluetooth Adapter.
I just want my Main Activity to be able to do that, is there any way to do that?
I got my problem fixed. It was actually pretty easy, on the tutorial about Providing Up Navigation on developer.android.com
I just put this code in my onOptionsItemSelected method
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
Also I made the launch mode of my Parent Activity as <singleTop>
android:launchMode="singleTop"
What it does is rather than creating a new instance of your parent activity, it just brings it to the top of the stack.
I've a Preference activity for a options menu in my Android app.
I've enabled the Up \ Back navigation on the ActionBar and I need to come back to the previous activity that called the Options menu.
For the Preference activity, I could use in the manifest:
android:parentActivityName="mypackage.com.MainActivity"
but how come back to other activies ? The Options menu is called from 4 different activities.
public class Prefs extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
Make sure that you have declared the parent activity in the manifest like this...
<activity
android:name="com.myapp.SetPreferenceActivity"
android:parentActivityName="com.myapp.MainActivity"
>
and then make sure to add the case into your onOptionsItemSelected method...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
//Take me back to the main activity
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
if you prefer to have the up button point to a custom activity, you can just use an intent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent changeActivity = new Intent(this,OtherActivity.class);
startActivity(changeActivity);
return true;
}
return super.onOptionsItemSelected(item);
}
The default behaviour of the Back button is that it will get you back to the calling activity. The system maintains a back stack of activities while the user navigates the application. Do you need to override this functionality? Please be more specific.
Solved in this way:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
}
return true;
}
I have a pretty simple use case which seems to be working for all my Activities except one. All my Activities are calling getActionBar().setDisplayHomeAsUpEnabled(true);. The only place this isn't working is on an Activity I just added, which is also the only one I'm launching from the main activity's info menu. The actual OS back button works as expected in this case, but the Home button is not. Here's the menu logic that starts the activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.share:
share();
return true;
case R.id.support:
emailSupport();
return true;
case R.id.settings:
settings();
return true;
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
The problem is with the SettingsActivity, called in the menu by the settings() method you see in the switch above:
private void settings(){
Intent intent = new Intent(InfoMenuActivity.this, SettingsActivity.class);
startActivity(intent);
}
And the onCreate of the SettingsActivity is pretty simple too:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = this.getSharedPreferences(getString(R.string.preference_file), Context.MODE_PRIVATE);
setView();
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
}
I originally only had the first getActionBar() call, but added the second to see if it changed anything, but it did not.
What am I missing?
From the comments it seems that you don't know how to set the parent activity in the manifest and keep supporting older versions.
For API 16 you can use parentActivityName attribute but for older versions you'll have to add a meta tag:
<activity
android:name="com.example.SettingsActivity"
android:label="Settings Activity"
android:parentActivityName="com.example.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.MainActivity" />
</activity>
If all you want to do is override the up button functionality to act as a back button then don't forget that in every activity you'll have to override onOptionsItemSelected.
However remember that overriding any default and expected android behaviour is bad practice.