I have an Navigation Drawer (MainMenu.class config)
First MainMenu load NewRadioFragment then i click button at NewRadioFragment start to ViewPager, ViewPager have (PlayRadioFragment and BlogContentFragment) View through ViewPagerActivity ( FragmentActivity ).
Now, I want Back Button in Actionbar back from PlayRadioFragment to NewRadioFragment.
I need help !.
Update
private void setImageView(ImageView src, View rootView) {
image_disk = (ImageView) rootView.findViewById(R.id.base);
new Thread(new Runnable() {
#Override
public void run() {
final Bitmap bimit = Utils.loadBitmap(image_url);
image_disk.post(new Runnable() {
#Override
public void run() {
image_disk.setImageBitmap(bimit);
prg_loading.setVisibility(View.GONE);
tv_loading.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
}
});
}
}).start();
}
When i click function don't finish
have you tried this?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
NavUtils.navigateUpTo(this, upIntent);
}
return true;
}
return super.onOptionsItemSelected(item);
}
or
try this instead
#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);
}
or
call
Finish();
Related
so i have Activity A,B, and C. Activity A & B both go to activity C. When i am on activity C and I press the back home button on mySupportActionBar, I want to return to the state of activity (from the state i left it in) I came from. How would i accomplish this?
Here is my onOptionsItemSelected(). So currently, it goes back to the designated parent activity i assigned in manifest to avoid my app from crashing. Because the parent activites require strings from intents.
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if(id == android.R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
I would also love to accomplish this onBackPressed().
The back arrow button is the "home" button when you're in a inner activity so you could finish the inner activity or maybe just call the back button
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Use android.R.id.home
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You also can use setNavigationOnClickListener on toolbar to trigger back button.
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_filter_category);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//back button action
toolbar.setNavigationOnClickListener(view -> finish());
}
}
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
}
}
I have implemented code for action bar button and it works fine but after that my smart phone back button is closes or minimizes when I press it. Here is my Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
/**
* Action Bar Back Button
*/
getActionBar().setDisplayHomeAsUpEnabled(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);
}
#Override
public void onBackPressed() {
moveTaskToBack(true);
AboutActivity.this.finish();
}
public void onBackPressed() {
moveTaskToBack(true);
AboutActivity.this.finish();// you are finishing the activity
}
if you want to go back try this
#Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
}
I'm trying to start an activity from OptionsMenu, but it doesn't start. Why?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.main:
Intent intent = null;
intent = new Intent(MainActivity.this, Impostazioni.class);
this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't you end up returning early when you do?
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
You never reach the code for startActivity I think. If that's not the case, are you sure the menu item has id R.id.main? I would want to debug this code with a break point at the top of this method - then step through and see what gets called and what doesn't.
// Delay is in milliseconds
static final int DRAWER_DELAY = 200;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//open navigationDrawer
new Handler().postDelayed(openDrawerRunnable(), DRAWER_DELAY);
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
case R.id.main:
Intent intent = null;
intent = new Intent(MainActivity.this, Impostazioni.class);
this.startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Method to open the Navigation Drawer...
private Runnable openDrawerRunnable() {
return new Runnable() {
#Override
public void run() {
mDrawerToggle.openDrawer(Gravity.LEFT);
}
}
}
Hi I'm calling an activity with ImageButton click but it would not finish the current activity.
btn_home.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), GridActivity.class);
i.putExtra("feed", _rssFeed);
startActivity(i);
SwipeDetailView.this.finish();
}
});
However, if I click the home button on the Actionbar menu within the same class, it closes fine.
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You are calling finish on the View.OnClickListener context instead you need to do ACTIVITYCLASSNAME.this.finish ()