I have a drawer activity and a method that calls several fragments, when I select an option shows me a listview with buttons. Clicking a button sends me to a new activity that has a back button. I want to return to the previous screen, return to the option you select in the drawer activity.
public class DrawerOpcionesActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_menu);
....
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mostrarFragment(position);
adapterOpciones.setPositionCheked(position);
}
});
if(savedInstanceState == null){
mostrarFragment(0);
}
...
}
private void mostrarFragment(int position){
Fragment fragment = null;
System.out.println("opcion: " + position);
switch (position) {
case 1:
fragment = new LineasTransporteFragment();
break;
....
}
In the other activity...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.xxxxx);
actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
... //new Intent.. I dont Know who call the save option from activity drawerOptions
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I need to save the selected option, when I call a new Intent show me the selected option. I show the fragment of default position 0
You may use SharedPrefrence of android to save the state and access back the state. Please have a look for sharedPrefrence
Related
I have a navigation drawer activity with fragments and I will send each fragment to an activity. I have a problem if I select option 3 of my menu that is a fragment that will send me to an activity, but when I return with the Back button, it sends me to option 1, what I want is that I return to option 3.
How can I change this?
I tried to do it by parentActivity but it did not work
Thank you.
My navigation drawer activitywithfragments
When I click on the button, it sends me to an activity that is this
And in the activity I have a toolbar to return and what I want is to return to option 3 and not to 1.
What do I need to do in my code or what can I place?
My fragment code option 3
public class Mis_Aliados extends Fragment {
Button boton;
public Mis_Aliados() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.persona_mi_perfil, container, false);
boton=view.findViewById(R.id.buton);
boton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActividadEx.class);
startActivity(intent);
}
});
return view;
}
}
My activity code
public class ActividadEx extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pruebaactividad);
Toolbar toolbarback=findViewById(R.id.include);
setSupportActionBar(toolbarback);
getSupportActionBar().setTitle("Activity");
ActionBar actionBar=getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
In your ActividadEx add this method to handle toolbar back button press
I'm putting 1 intent extra to make sure you want to open Option 3 when you are back to Navigation drawer Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, NavgationDrawerActivity.class);
intent.putExtra("openOption3", true);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
On NavigationDrawerActivty inside onCreate()
you can check if Bundle has data or not. If its empty you can open Option1 fragment.
If it has data check it. and open Option3 Fragment.
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
if (bundle.getBoolean("openOption3", false)) {
//Use Fragment Transaction to Open Option 3 Fragment
} else {
//Open Option 1 Fragment Or any other Fragment
}
} else {
//Open Option 1 Fragment Or any other Fragment
}
Feel free to comment if you've any queries.
There is a left drawer and a fragment that changes when an item in the navigation drawer is selected. The problem is that, if the "Item A" is already selected, and the user select it again, the fragment is changed for the same fragment.
To prevent this behavior, I need to know which item is already selected. I was using a attribute mLastSelectedItemPosition, but i don't think this is a good solution. Is there any way to get the current checked item from the navigation drawer?
class OnNavigationItemSelectedListener implements NavigationView.OnNavigationItemSelectedListener {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
if (mLastSelectedItemPosition != menuItem.getOrder()) {
// Is not the same item, so I can change the fragment.
mLastSelectedItemPosition = menuItem.getOrder();
}
mDrawerLayout.closeDrawers();
return true;
}
}
}
I do the exact same thing, have a int variable holding the selected position.
One thing to keep in mind is to save this variable onInstaceChange, because if you don`t you might be out-of-date with your drawer.
So, with that said, some code:
private static final String MENU_ITEM = "menu_item";
private int menuItemId;
...
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
if (menuItemId == item.getItemId()) {
drawerLayout.closeDrawer(navigationView);
return false;
switch (item.getItemId()) {
....
}
...
drawerLayout.closeDrawer(navigationView);
item.setChecked(true);
menuItemId = item.getItemId();
return true;
}
});
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(MENU_ITEM, menuItemId);
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
this.menuItemId = savedInstanceState.getInt(MENU_ITEM);
}
Something like that !
I think this will help you.
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
Toast.makeText(getApplicationContext(),"You have selected " +position,Toast.LENGTH_LONG).show();
}
}
I have an app with tab navigation between fragments. One of these fragments has an option to open a new activity. When I use the built in device back button from this activity it goes back to the tabbed activity with the previous fragment tab selected.
I have added a back button to the action bar of an activity in my app by using:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and setting the parent activity in the manifest, but this button always navigates back to the first tab of the parent activity, rather than the one that was previously visible.
How can I make this back button behave in the same way as the device back button?
Do something like this:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
onBackPressed() method:
#Override
public void onBackPressed() {
super.onBackPressed();
}
Handle back event in this manner
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
in back press method
#Override
public void onBackPressed() {
Intent intent = new Intent(SecondActivity.this,TabbedActivity.class);
intent.putExtra("IsBack",true);
startActivity(intent);
}
in your tabbed activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
if(getIntent().getExtras().getBoolean("IsBack")){
//navigate to your desire fragment
}
}
ActionBar
Let me try and explain this, I have a dropdown Actionbar with the selectable items, Home, Contact Us and About.
When the main activity loads, the default item shown on the ActionBar dropdown is Home. OK.
When I select the second item (contact us), the CURRENT ACTIVITY (main activity) sets itself as “Contact Us” then..
..then the NEXT ACTIVITY (contact us) starts up but sets the ActionBar back to default “Home” instead of ‘contact us’
Please see my code below:
public class MainActivity extends Activity {
private boolean mNaviFirstHit = true;
….
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SpinnerAdapter sadapter = ArrayAdapter.createFromResource(this, R.array.actions, R.layout.dropdown_textcolor);
// Callback
OnNavigationListener callback = new OnNavigationListener() {
String[] items = getResources().getStringArray(R.array.actions)
#Override
public boolean onNavigationItemSelected(int position, long id) {
if (mNaviFirstHit) {
mNaviFirstHit = false;
return true;
}
if (items[position].equals("Home"))
{
System.out.println("Selected Home");
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}
if (items[position].equals("Contact Us"))
{
System.out.println("Selected Contact US");
startActivity(new Intent(getApplicationContext(),ActionContact.class));
}
if (items[position].equals("About"))
{
System.out.println("Selected About");
startActivity(new Intent(getApplicationContext(),ActionAbout.class));
}
I have the following problem.
I am implementing a sliding menu from jfeinstein10 and for each menu option, I give a call to a different fragment class. In my menu options, I have a choice called profile in which when clicked, I want a new fragment with preferences to be displayed. Normally, I can do this by using PreferenceFragment, but that does not work with the sliding menu as it expects a class of type Fragment.
Here is my COde on Item click.
#Override
public void onListItemClick(ListView lv, View v, int position, long id) {
Fragment newContent = null;
Class<?> cls = null;
switch (position) {
case 0: /*this is the one that I want to edit*/
newContent = new profileFragment();
break;
}
if (newContent != null)
switchFragment(newContent);
}
How can I show the preferences in a Fragment?
Normally I would do this:
public class profileFragment extends PreferenceFragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
But that doesnt work as it extends PreferenceFragment and not Fragment
Any ideas?