Conflict between activity home button and Dialogfragment home button id's - android

I have an acivity with appbar and a fullsecreen dialogfragment with appbar (that dialog is called from the activity).
I have set some action when the home button of the activity is pressed, and when the home button og the dialogfragment is pressed it shoud close the dialog.
I have notice that the two buttons have the same id (android.R.id.home). and aparently there is a conflict when the method "onOptionsItemSelected" is called because when I press the home button of the dialog it doesn't work, but if a remove the portion of the code on the activity (if id == android.R.id.home)
it works fine and the dialog dismiss.
What should I do?. is there way to prevent this conflict , maybe set a diferent id for the home button?
this is the method of the activity
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout) {
exitApp();
return true;
}
else if ((id == android.R.id.home) && searchActivated) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
toggle.syncState();
searchActivated=false;
reload_fragment_data();
return true;
}
else if ((id == android.R.id.home) && (!searchActivated))
{
drawer.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
this is the method of the dialogfragment
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_next) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
updateTitle();
return true;
} else if (id==R.id.action_previous)
{
pager.setCurrentItem(pager.getCurrentItem() - 1);
updateTitle();
return true;
}
else if (id == android.R.id.home) {
dismiss();
return true;
}
return super.onOptionsItemSelected(item);
}

You can apply and try this solution in activity.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout) {
exitApp();
return true;
}
else if ((id == android.R.id.home) && searchActivated && !(fragment instanceof DialogFragmentClassName)) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
toggle.syncState();
searchActivated=false;
reload_fragment_data();
return true;
}
else if ((id == android.R.id.home) && (!searchActivated))
{
drawer.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}

Related

Action on item in Toolbar?

I have ToolbarCustom that has two items and an item can be clicked to open or close the layout. I am wondering how can I use the same item for opening and closing layout?
This my Code, but the variable openLayout is not defined:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
linearLayout = findViewById(R.id.show);
boolean openLayout=true;
if (id == R.id.action_categure) {
if (openLayout) {
linearLayout.setVisibility(View.VISIBLE);
openLayout=false;
} else {
linearLayout.setVisibility(View.GONE);
openLayout =true;
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
boolean firstTimeClick = true; //global variable
boolean secondTimeClick = false; //global variable
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
linearLayout = findViewById(R.id.show);
if (id == R.id.action_categure) {
if (firstTimeClick) {
firstTimeClick = false;
secondTimeClick= true;
linearLayout.setVisibility(View.GONE);
} else if (secondTimeClick){
firstTimeClick= true;
secondTimeClick= false;
linearLayout.setVisibility(View.VISIBLE);
}
}
if (id==R.id.action_card){
return true;
}
return super.onOptionsItemSelected(item);
}
Is that what you want to achieve?Hope I helped you.

Dismiss dialog causes activity finish

My PanelActivity contains a recyclerView with a list of items. Each item has a click event. This click opens DetailsActivity.
DetailsActivity has a floatingActionButton that opens a full screen dialog (my class DetailDialogFragment extends DialogFragment).
DetailDialogFragmenthas an Up/Home button with a dismiss.
The problem: If the user performs a click over the Up button, the dialog is dismissed, but also DetailsActivity disappear, and the app returns to the PanelActivity.
Possible reason: Under the Up button of the dialog is the Up button of the DetailsActivity. Is it possible to fire two click events when a dialog is over an activity and both have an Up button on the same place?
Edit: To show some code.
Open DetailsActivity from PanelActivity (clicking one item in the recyclerView).
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("headerCode", headerCode.getText());
context.startActivity(intent);
Up button in DetailsActivity.
#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);
}
Open full screen dialog in DetailsActivity.
private void showCreateDetailDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
DetailDialogFragment newFragment = new DetailDialogFragment();
// The device is smaller, so show the fragment fullscreen
FragmentTransaction transaction = fragmentManager.beginTransaction();
// For a little polish, specify a transition animation
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity
transaction.add(android.R.id.content, newFragment)
.addToBackStack(null).commit();
}
And finally, Up button in DetailDialogFragment.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.save) {
validateForm();
return true;
} else if (id == android.R.id.home) {
// handle close button click here
dismiss();
return true;
}
return super.onOptionsItemSelected(item);
}
I haven't tested it but I think the problem is here, where you call dismiss(). You may need a reference to the DialogFragment first. I think technically you're just calling this.dismiss(); where this equals the Activity you're working in.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.save) {
validateForm();
return true;
} else if (id == android.R.id.home) {
// handle close button click here
dismiss(); // problem is with this call
return true;
}
return super.onOptionsItemSelected(item);
}
You could try something like this:
private DetailDialogFragment detailFragment;
private void showCreateDetailDialog() {
detailFragment = new DetailDialogFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();
}
and now inside onOptionsItemSelected():
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.save) {
validateForm();
return true;
} else if (id == android.R.id.home) {
// handle close button click here
detailFragment.dismiss();
return true;
}
return super.onOptionsItemSelected(item);
}
No, I think that's not possible, maybe is a problem of your device, test it on an Android Emulator or another device. Can you please share your code to try to help you?

Menu and options menu together

how can I create a mainmenu in the actionbar and also an "options" menu that will open on hardware button click?
Like Whatsapp. There's an actionbar with avatar, name and and icon and if you press "menu" button, it will appear another menĂ¹ with "Show contact, media, search" etc.
This is my actual code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calendar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_legend) {
// Open dialog
} else if(id == R.id.action_settings) {
startActivity(new Intent(this, SettingsActivity.class));
} else if(id == R.id.action_add) {
startActivity(new Intent(this, AddActivity.class));
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onKeyDown(int keycode, KeyEvent e) {
switch(keycode) {
case KeyEvent.KEYCODE_MENU:
// Here open the options menu
return true;
}
return super.onKeyDown(keycode, e);
}

Menu click item event

I added 3 menu items: Settings / About and Exit.
As you can see in the code, I made the Exit button to close the app, and I want that when I press the About button to open a new activity. How do I do that?
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.Exit){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
if(id == R.id.action_masterz_yuris_about){
Intent openAboutActivityIntent = new Intent(this, MasterZYurisAboutActivity.class);
startActivity(openAboutActivityIntent);
}
try this code
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
} else if (id == R.id.Exit){
finish();
return true;
}else if (id == R.id.about){
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}

Back button on fragment

How can i translate fragment when i click back button?
#Override
public void onBackPressed()
{
FragmentTransaction ft;
ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, new RMBTStartFragment());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
Thank you.
put your code into
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (id == android.R.id.home) {
// your code
}
else if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
and put getSupportActionBar().setDisplayHomeAsUpEnabled(true); in your onCreate

Categories

Resources