How to recreate the listview of a former ListFragment after pressing back button - android

I have a ListFragment with a listview in it, that contains a navigation-structure. By selecting an item of the list, the next navigation hierarchy stage should be displayed. And so on.
The next hierarchy stage is created by a new fragment (remove the old fragment and add the same fragment new via fragmant transactions by using addToBackStack), in which the arrayadapter of the new listview is updated to the items of the next navigation hierarchy. That work pretty fine.
I want the user to have the possibility to navigate back via the back button. And here start my problems. I have no clue how to save the listview so it could be recreated after using the back button.
I thought using onSaveInstanceState would be a good idea. But onSaveInstanceState ist not called when the transaction gets comitted. I checked it by placing a Log.d("..","...) in the method. It´s not called. (What I found out, is, that onSaveInstanceState is called when rotating from portrait- to landscape-view and vice-versa. But that´s no help for my problem).
So what would be the best idea to store the elements of the listview and get it back to recreate the listview after using the back button and the former fragment is getting created? All items are Strings that are stored in an ArrayList that is bound to the ListAdapter.
Here my code. I implemented an interface, via the click on an item in the listview calls a method in the parent-activity. In this method I first call a method to fill the already existing ArrayList (navigationsItems) with the new items that are content of the next navigation-stage:
The code of my ArrayAdapter:
arrayAdapterFuerNaviList = new ArrayAdapter<String>(
BRC_BoardOverviewActivity.this,
android.R.layout.simple_list_item_1,
navigationsItems);
In this method I first call a method to fill an ArrayList (navigationsItems) with the items of the next navigation-stage:
// load new itemlist to navigation-
public void inhaltAktuelleNaviListeInArrayLaden() {
navigationsItems.clear();
for (int i = 0; i < prefs.getInt("aktuelleNaviListe_size", 0); i++) {
(...)
navigationsItems.add(titel);
}
}
Then I call the method, with which I load the new ListFragment and bind the new ArrayAdapter to it:
// push navifragment to BackStack and create a new instance of it
public void naviFragmenteNeuLaden() {
// get a reference to the actual ListFragment
Fragment removefragment = frgm.findFragmentById(R.id.navi_container);
// initialize ne ListFragment
BRC_NavigationsFragment navifragment = (BRC_NavigationsFragment) frgm
.findFragmentById(R.id.navi_container);
// remove the old and bind the new ListFragment from/to the container ...
FragmentTransaction frgmta = frgm.beginTransaction();
frgmta.remove(removefragment);
frgmta.add(R.id.navi_container, navifragment);
// ... push the old ListFragment on the BackStack and commit ...
frgmta.addToBackStack(null);
frgmta.commit();
frgm.executePendingTransactions();
// ... bind the updated ArrayAdapter to the new ListFragment ...
try {
navifragment.setListAdapter(arrayAdapterFuerNaviList);
} catch (Exception e) {
// TODO
e.printStackTrace();
}
// ... and notify
try {
arrayAdapterFuerNaviList.notifyDataSetChanged();
} catch (Exception e) {
// TODO
e.printStackTrace();
}
}
As mentioned, forward-navigating is no problem. But when i press back button for e.g. in the 2nd navigation-stage, the former ListFragment gets loaded (verfified with Log.d("..."."...)-Messages in onCreatView() of ListFragment), but the former listview which was in it isn´t created.
I am currently working on a new approach. When you click on an item in the list, I write the current list, together with a reference to the current ListFragment into a vector based stack. Then I catch with "onBackPressed()"-method the press of the back button. So now when the button is pressed, I call on the already mentioned methods (see above), write the item-data from the stack back into the list, call via the stored reference the forme ListFragmt and bind the updated ArrayAdapter to it. This is work in progress actually. I will write the result of this approach when finished.

I solved the problem.
For the different navigation contents of the ListView I have defined a class "Naviliste", in which all the items are stored. If someone clicks on an item, then the a new listview with the correspondingly new list will be generated. These lists are indexed, so each list get´s a fixed ID assigned.
By clicking an item in the ListView, the ID of the current Naviliste is pushed onto a stack. After that a new fragment will be created an the new list bound to it. The same procedure repeats if the user goes a step further, and so on ...
If the user pushes the back button to go a step back, the following is done:
The push of the back button is catched via the onPressedBack()-method placed in the parent activity. Via this method an own method is called. This pulls the last ID that was pushed onto the stack and then builds with it in a new created fragment the former list.
At the same time I will run along a counter that counts in which navigation depth I am. In each navigation step forward it get´s inceased by 1. And in reverse just reduced by 1. With this counter I am able to query if the user is in the navigation-root (counter = 0) and once more pushes the back button to leave the current activity. If so, i call finish() and the parent activity with its fragments get´s closed. Voilá!
It works great. Took a little bit time to get over it. But now I am happy that my solution works. :)

Related

Opening Instance of Activity

I have an app that hold post information in an activity. in this activity related posts listed in bottom of post. User by clicking on related post can go to post activity and see that post info and related posts too.
As you can see in image, I have Activity A that holds post and it's related posts. When user Click on post I send user to Activity A with new post id and fill activity by new data.
But I think this is not Right way!
should I used Fragment instead of Activity?
Opening another Instance of an Activity on top of another is simplest way of navigating a content graph. User can simply press back, and go to previously opened content, until user reaches back to starting Activity, then the application closes. Though pretty straight forward, this particular approach has two issues:
It may happen that a lot of Instances of same activity are on the stack, utilising a large amount of device resources like memory.
You don't have a fine grained control over Activity Stack. You can only launch more activities, finish some, or have to resort to intent flags like FLAG_CLEAR_TOP etc.
There is another approach, that re-uses the same Activity instance, loads new content in it while also remembering the history of content that was loaded. Much like a web browser does with web page urls.
The Idea is to keep a Stack of content viewed so far. Loading new content pushes more data to stack, while going back pops the top content from stack, until it is empty. The Activity UI always displays the content from top of the stack.
Rough Example:
public class PostActivity extends AppCompatActivity {
// keep history of viewed posts, with current post at top
private final Stack<Post> navStack = new Stack<>();
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get starting link from intent extras and call loadPost(link)
}
private void loadPost(String link){
// Load post data in background and then call onPostLoaded(post)
// this is also called when user clicks on a related post link
}
private void onPostLoaded(Post post){
// add new post to stack
navStack.push(post);
// refresh UI
updateDisplay();
}
private void updateDisplay(){
// take the top Post, without removing it from stack
final Post post = navStack.peek();
// Display this post data in UI
}
#Override
public void onBackPressed() {
// pop the top item
navStack.pop();
if(navStack.isEmpty()) {
// no more items in history, should finish
super.onBackPressed();
}else {
// refresh UI with the item that is now on top of stack
updateDisplay();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
// cancel any background post load, release resources
}
}
I would choose:
activity/fragment depends on complexity with:
horizontal recyclerview with custom expanded card view
and inside this expanded card view second vertical recyclerview :)
Here's what you can try.
Create a PostActivity which is a shell for fragments. Inside this activity you can just replace fragments using FragmentTransaction.
Your PostActivity can now have a PostFragment which will hold post and related posts. Now on click of post you can replace PostFragment with PostDetailFragment with postID being sent to the new fragment as a bundle. The PostDetailFragment will now display details according to id.
Check here: http://www.vogella.com/tutorials/Android/article.html#components_fragments
By seeing the picture the way i would implement is i would have create an activity with a bottom listview for your items and on top there would be a framelayout for holding fragments . when user click on any list item i would load the respective fragment in the activity
It all depends on what you are trying to achieve. What would you expect to happen when the user touches the back button after going down a couple of levels? If you want to the application to exit, no matter how deep in the sequence they have gone, then the best solution in my opinion is to simply reload the same activity with the new data and invaliding the affected views. If you need the back button to take the user back to the previous data, then the next question would be if you are keeping track of the past data breadcrumb. If so, then just intercept the back button and load the previous data for as long as there is data in your stack, or exit if you get to the top. If you don't want to keep track of the previous data chain, then instead of loading one activity with the new data, you can start a new activity of the same class, but with the new data. Android with keep the track of activities and each back button touch would close the running activity and take the user to the previous activity. Choice of activity versus fragment is just yours. You can use fragments that hold the data that you want to change after each user touch, create new ones when needed, disconnect the previous ones, and connect the new ones. You will need to do some extra work to make sure the back button works correctly (depending on you want the back button to behave). Based on what I can tell, it is simpler to just have one activity and load new data when needed and keep a trail of data changes, if you need to be able to go back.
It can be achieved using activity alone. Though I preferred moving all related UI to fragment.
You can use Navigator class.
Here the steps:
1. Add Navigator Class
public class Navigator {
private static Navigator instance;
public synchronized static Navigator getInstance() {
if (instance == null) {
instance = new Navigator();
}
return instance;
}
public void navigateToActivityA(Context context) {
Intent activity= AActivity.getCallingIntent(context);
context.startActivity(activity);
}
}
2. Add the calling method to your Activity class.
public static Intent getCallingIntent(Context context) {
return new Intent(context, AActivity.class);
}
3. Call the activity with the following code in your caller activity.
Navigator.getInstance().navigateToActivityA(this);
I suggest that you read about AndroidCleanArchitecture
For this task...
0) Starting new activity
I read again about question, and understood that you need advice for starting activity. So, starting new activity it's Ok, your main problem will be with another things (see below).
But lets talk about starting another data. Using Fragment instead doesn't resolve your task, fragments helps with different screen work. Using for example just data refreshing as a variant. You may use just single activity and refresh only data, it will look much better, if you also add animation, but not better than starting activity.
Using Fragment helps your with different screen actions. And maybe, answering on your question - it will be most suitable solution. You just use single acitivity - PostActivity, and several fragments - FragmentMainPost, FragmentRelated - which will be replaced, each other, by selecting from related post.
1) Issues with returning back
Lets imagine, that users clicks to new one activity and we loaded new data. It's Ok, and when Users clicks over 100 activities and receiving a lot of information. It's Ok, too. But main question here it's returning back (also another about caching, but lets leave it, for now).
So everyone know, it's bad idea to save a lot of activities in stack. So for my every application, with similar behavior we override onBackPressed in this activity. But how, lets see the flow below:
//Activities most have some unique ID for saving, for ex, post number.
//Users clicks to 100 new activities, just start as new activity, and
//finish previous, via method, or setting parameter for activity in AndroidManifest
<activity
noHistory=true>
</activity>
....
//When activity loaded, save it's activity data, for ex, number of post
//in some special place, for example to our Application. So as a result
//we load new activity and save information about it to list
....
// User now want return back. We don't save all stack this activities,
// so all is Ok. When User pressing back, we start action for loading
//activities, saved on our list..
.....
onBackPressed () {
//Getting unique list
LinkedTreeSet<PostID> postList =
getMyApplication().getListOfHistory();
//Getting previous Post ID based on current
PostID previousPostID = postList.get(getCurrentPost());
//Start new activity with parameter (just for ex)
startActivity(new Intent().putExtra(previousPostID));
}
RESULT
I found this as the best solution for this tasks. Because in every time - we work only with single activity!

fragment does not reflect changes

I am trying to create an application in which my Fragment contains one listview which is populated from a database, and when one of the item is clicked, a new activity starts.
In this new activity, I update the database, and then come back to the first Fragment (by pressing back button).
What happens next is that the ListView in the first Fragment does not take the changes I made in the second Activity into account.
And strange thing is that when I start the application again, the Fragment shows the updated list.
How can I automatically update the ListView when making changes to the database?
Try to use the onResume( ) method in your Fragment, so you can told the Adapter that the Dataset has changed.
#Override
protected void onResume() {
super.onResume();
// Dataset has changed, notify adapter !
mAdapter.notifyDataSetChanged();
}

What's the correct way of displaying ViewPager after associated ListView's item click?

I'm a beginner in Android, so I apologize for the mistakes and I'd appreciate any constructive criticism.
I'm writing a basic application with a ListView of images, and when the user clicks on an item in the list, I want to display that image in a ViewPager, where the user can swipe back and forth to browse the whole list of images. Afterwards when the user presses the back button, I want to switch back to the ListView.
I manage the business logic in the MainActivity, which uses MainActivityFragment for the ListView and ImageHolderFragment for ViewPager.
The simplified code so far is as follows:
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListItems = new ArrayList<>();
mListItemAdapter = new ListItemAdapter(this, R.layout.list_item, R.id.list_item_name, mListItems);
mListView = (ListView) findViewById(R.id.list_view_content);
mListView.setAdapter(mListItemAdapter);
mDeletedListItems = new ArrayList<>();
mViewPager = (ViewPager) getLayoutInflater().inflate(R.layout.image_display, null, true);
mImageAdapter = new ImageAdapter(getSupportFragmentManager(), mListItems);
mViewPager.setAdapter(mImageAdapter);
mViewPager.setOffscreenPageLimit(3);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mViewPager.setCurrentItem(position);
setContentView(mViewPager); // TODO: this is very wrong!
}
});
loadImages();
noContentText = (TextView) findViewById(R.id.no_content_text);
if (mListItems.isEmpty()) {
noContentText.setText(R.string.no_images);
} else {
mImageAdapter.notifyDataSetChanged();
}
}
Although this does work to some extent, meaning that it manages to display the ViewPager when an item in the list is clicked, there are two things about it ringing the alarm bells:
I've read that calling setContentView() for the second time in the same class is pretty much a sin. Nobody explained me why.
The back button doesn't work in this case. When it's pressed, the application is terminated instead of going back to the list view. I believe this is connected to the first point.
I would appreciate any help, explanations if my idea is completely wrong, and if my case is hopeless, I'd like to see a successful combination of ListView and ViewPager with transitions between each other.
Your activity already has R.layout.activity_main set as content view, which rightly displays the list view - that's what the responsibility of this activity is as you defined it. If we want to change what's shown on the screen, we should use a different instance of a building block (activity or fragment) to display the view pager images.
To say the least, imagine if you wanted to change the view to a third piece of functionality or UI, or a fourth... it would be a nightmare to maintain, extend and test as you're not separating functionality into manageable units. Fields that are needed in one view are mixed with those needed in another, your class file would grow larger and larger as each view brings its click listeners, callbacks, etc., you'd also have to override the back button so it does what you want - it's just not how the Android framework was designed to help you. And what if you wanted to re-use UI components in different contexts whilst tapping in to the framework's activity lifecycle callbacks? That's why fragments were introduced.
In your case, the list view could continue to run in your MainActivity and in your click listener, onItemClick you could start a new activity that will hold a viewPager:
Intent i = new Intent(MainActivity.this, MyLargePhotoActivityPager.class);
i.putExtra(KEY_POSITION, position);
// pass the data too
startActivityForResult(i, REQUEST_CODE);
Notice how you could pass the position to this activity as an int extra, in order for that second activity to nicely set the viewPager to the position that the user clicked on. I'll let you discover how to build the second activity and put the ViewPager there. You also get back button functionality assuming your launch modes are set accordingly, if needed. One thing to note is that when you do come back to the list View, you'd probably want to scroll to the position from the view pager, which is why you could supply that back as a result via a request code. The returned position can be supplied back to the list view.
Alternatively, you could use the same activity but have two fragments (see the link further above) and have an equivalent outcome. In fact, one of your fragments could store the list view, and the second fragment could be a fullscreen DialogFragment that stores a viewPager, like a photo gallery (some details here).
Hope this helps.
I've read that calling setContentView() for the second time in the
same class is pretty much a sin. Nobody explained me why.
Well, you kind of get an idea as to why.
When you use setContentView() to display another 'screen' you do no have a proper back stack.
You also keep references to Views (like mListView) that are not visible anymore and are therefore kind of 'useless' after you setContentView() for the second time.
Also keep in mind orientation changes or your app going to the background - you'll have to keep track of the state that your Activity was in which is way more complicated than it has to be if you have one Activity that does two different things.
You won't be arrested for doing things like you do right now, but it's just harder to debug and keep bug free.
I'd suggest using two different Activities for the two different things that you want to do, or use one Activity and two Fragments, swapping them back and forth.
If you insist on having it all in one Activity you need to override onBackPressed() (called when the user presses the back button) and restore the first state of your Activity (setContentView() again, pretty much starting all over).

TouchListView is not working when parent activity finishes

I'm using a TouchListView by CommonWare and so far it's working fine, But it fails in the following case:
activity A has a TouchListView and button to add an item , now when i click over a button,
activity B opens up and once the user choose the item i am calling finish to end activity B
and it comes back to activity A.
Now here drag n drop in TouchListView doesn't work I mean items are getting dragged but not able to be placed where a user wants, I tried a lot but could not find a way to fix it.
I suspect you're doing some setup in onCreate() that's not getting called when you return to Activity A because you're entering it the 2nd (and subsequent) time(s) via onResume().
http://developer.android.com/reference/android/app/Activity.html
BaseAdapter adapter;
List<myStuff> stuffs;
if (adapter == null) {
adapter = new BaseAdapter()
if (stuffs == null) {
stuffs = new List<myStuff>();
}
}
You're going to need to do something like that and add List to your savedInstanceState bundle in onSaveInstanceState() then dig it out when the activity restarts.

Restoring the Fragment

My application has one activity and 5 fragments. When user clicks on tab "Fragment1", it loads the data from remote place and upload it on ListView. "Fragment2" also doing same job. But when user clicks back on "Fragment1" it again starts loading, instead of loading
How can I restore the fragment from last loaded state?
It might help if you post some code. Meanwhile, my sneaky suspicion is that instead of reusing the same instance, your FragmentPagerAdapter is creating a new instance every time its getItem() is called.
Perform a check in OnStart() of the fragment.
if(listView has Content || is not null)
{
//get the data from remote
}
else
{
//don;t do anything
}

Categories

Resources