I am beginner in android and I am working on a small project.
There is multiple fragment in that and I'd like to add a back button in fragment to go back.
How can i do that?
create a back button in your fragment layout, then get a reference of it in your fragment class as shown below.
Button backButton = (Button)layout.findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
Related
I have an activity with a Viewpager and two buttons inside.
The two buttons are "next" and "back".
I made some Fragment layouts and now want to change the shown Fragment by clicking one of those buttons.
I just know how to use the FragmentStatePagerAdapter to slide between fragments, but how can I do this via onClick?
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do
Yourpager.setCurrentItem( page )
}
});
In my Application, i'm transitioning to a DetailsFragment when the user clicks on a list item. and there are TWO options to be back at the main Fragment (the List Fragment).
Press the back button. (no problem here, because I handle this in onBackPressed() in MainActivity)
Press the Toolbar back arrow (Here is my problem).
When the user presses the toolbar back arrow, I call the following
getActivity().getSupportFragmentManager().popBackStack();
how can I intercept this event in MainActivity?
(There are some manipulations I am doing in MainActivity when the List Fragment is visible to the user.
Just put this code
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
I have tabbed activity using fragments and in one of them i have a button, but i can only use in the fragment itself but now, I want to use it in my MainActivity?
is there a way to have a Fragment's button listener on activity?
there are two methods
You can write a Button click event callback in the fragment
You can write in activity
((Button)getFragmentManager().findFragmentById(/*frgment id*/).getView().findViewById(R.id.one_nextpager)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
i created one tab menu.when i click the tab it opens one activity under the tab that activity contains two buttons.when clicking the buttons it open the new activity it does not open a activity under that tab itself.how should i open a another activity under the same menu.please help friends.
i used this code to open a another activity when button was clicked
button1.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(SongsActivity.this,FiveActivity.class);
startActivity(intent);
finish();
} });
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent obj=new Intent("com.layout.Contactregistration");
startActivity(obj);
}
});
If i understood your question then You can use simple logic for your button click.
First you can change your layout on button click that you are trying to open in a new activity.for this you can use
setContentView(R.layout.SecondLayout);
again on button click and initialize your FiveActivity's resources and use them.
this saves your memory and comfusion of activities.
I have an application that has a menu and depending on what button you press in the menu a new activity is opened. I want to have a back button on every screen that will bring you to the previous screen so I'm wondering how do I go about this?
Here is some code I have used that works:
backButton = (ImageButton) findViewById(R.id.back_button);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
However its not good programming practice for me to put this code in all my activities. How do I go about creating some kind of stack that saves all the pages viewed and using that to return to the previous page?
I have to put a back button in my application so I cannot use the existing one in the ActionBar.
just an Idea
Create a baseClass which extends Activity. In there declare
#Override
public void onClick(View v) {
super.onBackPressed(); // or super.finish();
}
In all activity extend this Base Class. And in every layout in the button put
android:onClick="onClick"
And to make the xml design of button reusable create it in a separate xml. and add it using <include/>
Have you tried using action bar in your activity ? use in every activity
ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
actionBar.setTitle(getResources().getString(R.string.app_name));
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(R.drawable.app_icon);
}
and handle in
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I would suggest against having a custom back button. Android has a hardware back button. Pressing the haradware back button will navigate to the previous.
I don't think you need a custom back button. I don't think its a good programming practice to override the default behaviour.
You create a backbutton in your activity and implementing the functionality as you have done above. Still the user can use the hardware back button for the same functionality. So you would be providing the same functionality which is redundant.
There is a hardware back button on all android devices, and it does exactly what your lines of codes do, unless overridden to do something else.
You can refer to this answer as well.
I had a similar issue where I was using a back button "Cancel" and a home screen "homeActivity". I ended up solving it using this:
CancelButton = (Button) findViewById(R.id.cancel_button);
CancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent homeActivity = new Intent(getApplicationContext(), DeviceListActivity.class);
startActivity(homeActivity);
finish();
}
});
In kotlin
backButton!!.setOnClickListener(View.OnClickListener {
onBackPressed()
})