Starting activity causes ViewPager to reset - android

I asked this question previously and thought it was working and deleted the question, but since have found this isn't working. I have a ViewPager in my app, with each Fragment of the ViewPager holding a ListView. When an item in the ListView is selected, I am starting a new activity in onListItemClick:
#Override
public void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
final Intent browser = new Intent(activity, Browser.class);
startActivity(browser);
}
The problem is that when I press either the back button, or the Home button in the Actionbar, the ViewPager has reset itself to show the first fragment.
This is the code I'm using for the Home button:
#Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, Main.class);
startActivity(intent);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
It was recommended, in my original question, to set the launch mode in the Android Manifest to singleTop, which I did for the startup activity as well as the activity launched from the ListView, but that didn't seem to fix it.
I thought about passing the ViewPager's position to the new activity, then retrieving it in onResume of the original activity and manually setting the ViewPager's position, but that seems really brute force to me.

Related

How to send intent data back to previous activity when back arrow icon is pressed?

I have an Android app that displays elements in a GridView: each of these elements is clickable and starts an Activity with its details; then you can go through another activity from the second one to add more data.
My question is: when I go back from 3rd to 2nd activity, my app crashes (and I know this is because going from 3rd activity to 2nd one, the 2nd so called hasn't got the intent data that it needs).
What can I do to solve this issue?
My Gridview calling the 2nd activity
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), PokemonDetails.class);
i.putExtra("id", position);
startActivity(i);
}
});
My 2nd activity calling the 3rd:
pokeDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MyPokeDetails.class);
startActivity(i);
}
});
you can just override the onBackPressed() method with the same function of your buttons.
#Override
public void onBackPressed() {
//put Intent to go back here
}
Your activity should be recreated from the last state when you go back to it. Do you check the intent you have your data in in your 2nd activity to be non null? I guess the app could crash because of that.
You could also work with the savedInstanceState.
Override onSaveInstanceState and put the id you need into the bundle. If onCreate of your second Activity gets called, look if Bundle is non null and go get your value.
Further info: https://developer.android.com/training/basics/activity-lifecycle/recreating.html
You should use startActivityForResult() instead of startActivity() For details see this answer and Official documentation

Android Navigate Back to Activity; Don't Reload Parent

I have a scenario where I am clicking on a ListFragment and spinning up a new Activity like below:
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), VenueBeerActivity.class);
Parcelable wrapped = Parcels.wrap(mAdapter.getItem(position));
intent.putExtra("venue", wrapped);
startActivity(intent);
}
This works fine and displays the new activity.
I've modified this activities manifest so it points back to its parent activity (in this case main activity)
However the problem I have is when the back button is pressed, it reloads the entire parent. The parent is a list and I don't want it to reload the users position. How can I prevent this?
As a note. The parent houses a Page Tab Strip.
I'm sure this is a relatively simple fix...
What do you mean by "back button"? Is it the up button in the toolbar? If that's the case, edit the onOptionsItemSelected in your VenueBeerActivity to:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
So when user press the Up button it will get the behavior of the back button in the navigation bar.
In the parent activity, set the android:launchMode attribute to singleTop. This would prevent the system from creating a new instance of parent when up button is pressed.

How to make drawer just slide close instead of creating the activity again?

I have an application where onItemClick() activity determines which item in the drawer is clicked and the corresponding activity is started. However, when if I am already at the 'Settings' screen and I open the drawer and click on 'Settings' again, it starts a new settings activity. How can I optimize my code here to just detect that user was already on the Settings screen and therefore the drawer should just slide close?
Here's my code:
//#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
Drawer.closeDrawers();
Intent i = new Intent(DrawerActivity.this, SignUpActivity.class);
switch(position){
case 7:
i = new Intent(DrawerActivity.this, UserSettingActivity.class);
break;
}
startActivity(i);
For that you can store that last clicked number in one variable.
Then next time do check whether current clicked position is not equal to last store position
If it's not equal launch new screen and updated last clicked position else don't do nothing

Opening a new fragment when a ListView item is clicked

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.

Unable to implement navigation drawer properly

I am using the sample code from the developer.android.com for navigation drawer. I am unable to know what exactly needs to be changed to start activities instead of images which now appear. So what parts do I need to delete so that I can make listviewitemclick to open activities?
I am working here
private void selectItem(int position) {
switch(position){
case 0:
Intent a = new Intent(MainActivity.this, sampleopen.class);
startActivity(a);
}
}
The problem is that it opens the second activity first then when we press back it goes to first activity and there the drawer is implemented
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
Here you define a listener - you need to provide your own instead of new DrawerItemClickListener() and there you'll be able to launch activities according to position received.
// set the on item click listener for the listview object
mNavigationListView.setOnItemClickListener(mOnNavigationItemClickListener);
// handle clicks here
private AdapterView.OnItemClickListener mOnNavigationItemClickListener = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) then launch ACtivity #1
//....
}
};

Categories

Resources