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
Related
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);
}
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?
I have an activity and in that i have a textView,Framelayout(having fragments) and a button. while clicking on the button , i am changing the fragments in framelayout and text in textView (according to fragments). On pressing the back button of my phone i am somehow able to get previous fragment with onBackpressed function but not able to get the previous value on the textView. how to do it.
public class Quetionare extends AppCompatActivity {
Toolbar toolbar;
TextView tv;
Fragment fr;
Button next;
intro into;
SelectCar selectCar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quetionare);
initToolbar();
next=(Button)findViewById(R.id.button2);
tv = (TextView)findViewById(R.id.textView5);
fr = new intro();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.commit();
}
private void initToolbar()
{
toolbar = (Toolbar)findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
}
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
public void selectFrag(View view)
{
if(view==findViewById(R.id.button2))
{
fr = new SelectCar();
next.setVisibility(View.GONE);
tv.setText("Select Your car model and Plan");
}
else
{
fr = new Checkbox();
tv.setText("Make Your Plan");
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
#Override
public void onBackPressed()
{
if(getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
if (fr == into)
{
next.setVisibility(View.VISIBLE);
}
else
{
next.setVisibility(View.GONE);
}
}
else
super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_quetionare, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
You will have to save the value in the TextView when transitioning to your second fragment to a field, and then call setText using that value when you are navigating back.
we need to use findfragmentbyid method. By using this i have achieved desired functionality. thanks
I have been searching for showing fragments but the problem still exists and I can't get it to work.
my problem is when ever I add repeat_entry to menu for showing another fragment, that fragment is not showing but when I click on action_settings the fragment for that one is showing.
#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();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
selectTabToShow(String.valueOf(R.id.action_search));
CategorySettings fragemnt = (CategorySettings) getFragmentManager().findFragmentById(R.id.categorySettings);
fragemnt.refresCategoryList();
Fragment fragment = new CategorySettings();
ft.add(R.id.category_cont,fragment,"asdf");
ft.commit();
return true;
}else if(id == R.id.repeat_entry){
Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
Fragment fragment = new RepeatEntry();
ft.add(R.id.category_cont,fragment,"repeat tag");
ft.commit();
Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
As of now this is my RepeatEntry fragments
public class RepeatEntry extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.repeat_entry,container,false);
return v;
}
}
Please help me guys I'm new to this.
Try to replace
ft.replace(R.id.category_cont,fragment,"repeat tag");
I'd like to replace a fragment, when a menu button is clicked. The toast is displayed, but it seems like I have a problem with the FragmentTransaction, since it tells me it
Description Resource Path Location Type
Cannot instantiate the type FragmentTransaction ActivityAppLaunch.java /juraQuiz/src/com/pthuermer/juraquiz line 71 Java Problem
this is what I tried:
#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.actionbar_menu_info) {
Toast.makeText(getApplicationContext(), "Info", Toast.LENGTH_LONG).show();
FragmentTransaction ft = new FragmentTransaction();
Fragment fragment = new FragmentInfo();
ft.replace(R.id.container, fragment);
}
if (id == R.id.actionbar_menu_settings) {
Toast.makeText(getApplicationContext(), "Settigns", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
try FragmentManager
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new FragmentInfo();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();