Opening a new fragment when a ListView item is clicked - android

I'm pretty new to Android so I may be doing things completely wrong but I'm trying to get a fragment to open up when a list item is clicked and then to close it when it is dismissed with the back button.
I now have something working but when the back button is clicked it redraws the whole initial view. SO the listview scrolls back up to the top and reloads all the items from the db.
On the list item click in the main activity I am doing this
public void onItemSelected(String id) {
Intent detailIntent = new Intent(this, DetailActivity.class);
detailIntent.putExtra(Fragment.CLICKED_ID, id);
startActivity(detailIntent);
}
Then in the activity I am using this to show it.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_day);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
OpenDayFragment fragment = new OpenDayFragment();
getFragmentManager().beginTransaction()
.add(R.id.open_day_detail_container, fragment)
.commit();
}
}
and this to close it.
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
AS i said it all works but the listview and all the tabs from MainActivity seem to be completley disposed of when the activity is run. Is there a way just to show a fragment over the top and dismiss it, leaving the previosu activity intact?
Thanks

On close button you are recreating the home activity which will as a result launch every thing again. To void that you should finish the DetailActivity that will take you back to the Home Activity.

It reloads it again because you are creating the MainActivity again. To avoid that try finish(); in if (id == android.R.id.home) of your DetailActivity so it closes it, keeping the preview state of the MainActivity.

Related

Navigation view handle back button on fragment

Hi I need to handle back button on fragment. I using this navigation on my mobile apps. The question is how to handle back button when I open new fragment page?
I try using below code on new fragment.
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
But when click the back button , the navigation will be open. Any solution ?
Thanks
As you know whenever user presses Back button you need to go to the previously loaded fragment and to do that try this (note fragment must be added with transaction.addToBackStack(null);)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
int backStackCount = fragmentManager.getBackStackEntryCount();//check currently how many frags loaded
if (backStackCount > 0) {
fragmentManager.popBackStack(); //go back to previously loaded fragment
}
}
return super.onOptionsItemSelected(item);
}

Android back-button working but back via home button not working perfectly

I have a recycler view with some data. when an item clicked, opens new DetailsActivity.
My trouble is when it come back from DetailsActivity to MainActivity with Back-Button (Down on the Natel) working perfectly. But when I come back via home button extends ActionBarActivity, load the data again from scratch. I want exactly the same return as back button in Natel below.
does anyone have an Idea?
Back Code in onOptionsItemSelected Methord:
if (id == android.R.id.home) {
Intent homeIntent = new Intent(this, ActivityMain.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
// NavUtils.navigateUpFromSameTask(this); // also tried
}
If I understood correctly, instead of starting the activity over, just call finish() on your current activity.
Basically when you click an item you are calling the below correct (or something along those lines)?
startActivity(new Intent(this, DetailsActivity.class);
If so, then once you are in your Detailsactivity, just call:
if (id == android.R.id.home) {
finish();
}
That will finish the Detailsactivity which was shown over your MainActivity and return you to the exact same spot you were at.

Which method is called when back button is pressed from a fragment

I've had a look at this question-
Call OnResume when Back Button Pressed when using Fragments
I've done the same things, as mentioned in the answers, but neither is onResume called, nor onCreateView. It's a transaction from an activity to a fragment, all of which are a part of a single tab. How can I call a method from the first fragment when back button is pressed on the second fragment?
Code-
Bundle data = new Bundle();
data.putString("q", code);
data.putString("type", type);
data.putString("name", name);
viewPager.setCurrentItem(1);
android.support.v4.app.FragmentTransaction transaction =
getSupportFragmentManager().beginTransaction();
eq equity = new eq();
equity.setArguments(data);
transaction.replace(R.id.root_frame, equity).addToBackStack(null).commit();
The method called when you press the back button is onOptionsItemSelected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
if (yourCondition = true) {
//do something here
} else {
super.onBackPressed();
}
}
But onOptionsItemSelected is called on the parent activity.
I don't know if the back button is required, but I think it's better to implement a callback from one fragment to the other with an Interface and using a custom button not the back button.
fragment life cycle is directly effected by the host's activity life cycle so you gotta take that in consideration anyway did you try to call the method in onPuase() or in onStart() in the fragment that you are heading for after pressing back ? if that didn't work try to reach the emulator back button from if (keyCode == KeyEvent.KEYCODE_BACK) or you can finish() the previous fragment or activity so the back button will get the user no where and create your own back button to redirect the user and replace the method in it using onClick() .
hope that will work for you good luck

How to finish activity with custom animation when home button pressed

I have an ActionBarActivity "B" whose parent is ActionBarActivity "A" (also defined in manifest). A is in "singleTask" launch mode. I have an animation when starting B from A as follows:
public void onItemClick(...) {
Intent mIntent = new Intent(getActivity(), B.class);
startActivity(mIntent);
getActivity().overridePendingTransition(R.anim.B_in, R.anim.A_out);
}
On B, I have the following onOptionsItemSelected and onBackPressed:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getSupportFragmentManager().popBackStackImmediate();
//onBackPressed();
//finish();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.A_in, R.anim.B_out);
}
Here is the problem: When I press back button, the animation at onBackPressed is taking place as expected. However, when I click on icon at top left in the actionbar, popBackStackImmediate is called and Android's default animation is played which is different. So:
How can I manage to get same animation as in onBackPressed?
Should I use onBackPressed() instead of popBackStackImmediate()? Will it give the same result as popBackStackImmediate do?
Any suggestions and best practices are welcome...
You could use .popBackStack() instead of popBackStackImmediate() then overrride the pendingTransition, that might work. Since these are both activities though, my inclination would be to call finish(); then overridePendingTransition().

Pressing back button calls onCreate (Android)

Activity A starts Activity B. In Activity B, I have this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this,
ArticleListActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
When I press that home button, it takes me to Activity A as it should. However, onCreate is being called again. I do not want this behavior.
I'm guessing its because this implementation uses new Intent to previous item in the navigation stack. This is just code I got from Eclipse when creating the double pane project though. I looked around on stack overflow, and though it seems that using an Intent to go back is causing this behavior, I do not understand why Google would provide this in a default template.
How should I make this call differently so that onCreate is not called again when going back to Activity A?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
just finish current activity it will take you on previous activity
if your previous activity is activity A then just use finish(); method instead of creating object of intent
As pointed out by #Rajesh you need to call onBackPressed().
Your code detects the home button press and creates a new ArticleListActivity class.However, you don't want to create a new class you only want to go back to your already created class/activity. so use onBackPressed() instead.
You can try to set the launch mode in the manifest, because the parent activity can be popped off the stack.
android:launchMode="singleTop"
In this way, the onCreate() is not called again.
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}

Categories

Resources