Android Fragments Best usage for memory - android

I have a Main Activity with 5-tabs (like Instagram) and because I didn't want to load each of them everytime the user clicks a tab , I used and set the "set Screen Page Limit" command to 5.
However , I understand it is very heavy for memory .
What I've seen a just few seconds ago on the facebook app is that it doesn't auto load the content of all tabs but every tab's content loads when you visit it and by the time a tab is loaded , next time you visit the specific tab , it doesn't load again.
How can we achieve like this ?
Thanks!

First you need to enable cache in api library Like use volley network api. and all operation start onCreate() method like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_activated_1, android.R.id.text1, DummyContent.ITEMS));
}
because its called once and second time this method will get called and you will get your data as its on second visit.

Related

Android activity loses data when switching between apps

I'm writing my first Android app. Essentially it shows and controls the score of a straight pool match. I've read articles about the lifecycle of an activity but I still have a problem I don't understand:
When I switch back to my app with a running game (so a score different from the initial 0:0 is shown) the activity sometimes loses its state and shows 0:0 instead of the score when I left the app. I overloaded the methods onSaveInstanceState and onRestoreInstanceState. The former get's called when I press the home button of my device. The latter never gets called. I've read that the method only gets called when onCreate is called. The onCreate method doesn't get called although the app needs longer than usual to reload after switching to some other apps in between. So I think the activity does get rebuild but obviously not by onCreate and the saved score is not loaded by onRestoreInstanceState.
Can you explain me what's happening and how to achieve the desired behaviour? Thank you very much!
edit: I was asked to post my onCreate() and onSaveInstanceState() methods. I tried to shorten them in a useful way. Please tell me If there is anything unclear or missing.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GameSetting gameSetting = getGameSettings();
final GameData gameData = new GameData(gameSetting.getLeague());
game = new GameLogic(gameData);
scoreViews.put(PlayerId.PLAYER_A, (TextView) findViewById(R.id.playerAScore));
}
#Override
protected void onSaveInstanceState(#NonNull final Bundle outState) {
GameDataInstanceSupplier.saveInstance(game.getGameData(), outState);
// inside the method the data gets saved like
// outState.putInt(STATE_SCORE_A, data.getScore(PlayerId.PLAYER_A));
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(#NonNull final Bundle inState) {
super.onRestoreInstanceState(inState);
GameDataInstanceSupplier.restoreInstance(game.getGameData(), inState);
// inside the method the data gets loaded like
// data.setScore(PlayerId.PLAYER_A, inState.getInt(STATE_SCORE_A));
}
I put a "debug output" inside the onCreate() method and it did not appear in the debug log. The same was true for onRestoreInstanceState(). A message was printed inside onSaveInstanceState() when I pushed the home button.
I think your app is not the problem, you see, I came to find this page because I have the same problem switching aplications in my android phone, it's specially noticiable when usig 2 factor authentication and have to switch to text message, mail or other app to get the sent code, when switch back to the code input screen on the browser or app (teams for example) it is no longer there anymore and it has return to the begining, the login screen for example. This is very annoying because I have disable every thing that has to do with energy savings but still have the issue. I discovered that if I open first the message app and then do the authentication while switching very quickly just to see the code in 1 or 2 seconds and then return sometimes it works. It could be memory issue, or may be an unfulfilled politic on the energy savings. I think is probably the last because other explicitly stated properties like setting the screen brightness automatic adjust off and set to maximun works for a while and then returns to a default. It may even be a planned obsolescence. My phone is a Huawei P smart 2019 (POT-LX3) with android 10. I'll may be try a factory reset. Any suggestions are welcome.

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).

How to pre-load a fragment before showing?

In my activity I have several fullscreen fragments, each of them downloads some data from web (using an async task) and shows them to the user. The fragments are showed one at a time.
To be more specific, each of the fragment readings some urls from a sqlite database, and fetch the content before showing them in a list, if that matters. The data loading tasks can be done in the OnCreate() function.
I would like to preload all the fragment (at least starting the downloading), when I show a splash screen. Pretty much like a viewpager preload its fragments.
I am wondering how to achieve this? I tried initialize/create all the fragments in the OnCreate() function of my activity, hoping the OnCreate() of fragments could be called earlier, but the OnCreate() and OnCreateView() function of the fragments are not called until a fragment is about to show to the user.
It sounds like you need to separate your model (the data which is downloaded) from your view (the fragments). One way to do this is to start the downloading AsyncTasks in your activity, rather than starting them in each fragment. Then when the fragments are eventually displayed they can show the data which has been downloaded (or a spinner or some other indication that the download process is still executing).
Fragment's onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
So your solution to this problem is initialize or create or do your stuffs which you want to preload before fragments are created, inside your Fragment's onActivityCreated(Bundle)
see documents for fragment's lifecyle
The earliest pace you can start loading is either in a static singleton or in the Application Class
What I end up doing is the following, (1) add all the fragments into the container. So they (and their view) will be created and initialized. (2) hide those not in use and only show the one I would like the user to see. (3) use FragmentTrasaction.show()/FragmentTrasaction.hide() to manipulate the visibility instead of FragmentTrasaction.add() or FragmentTrasaction.replace().
If you following this approach, be warn that all the fragments will be cached in memory. But the benefit is the switch between fragment will be fast and efficient.
I was facing the same problem and then I used this method, suppose we are having an EditText in the fragment, then we can use codes like this
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//this method allows you to input or instantiate fragments before showing this to an activity conidering id is "editTextEditProfileFirstName"
EditText firstName = (EditText) getActivity().findViewById(R.id.editTextEditProfileFirstName);
firstName.setText("This is my first name", TextView.BufferType.EDITABLE);
super.onViewCreated(view, savedInstanceState);
}

Forcefully display a empty layout and then fill it with data

I am developing a new app for android 2.2(Froyo), I need to know how to forcefully display a leyout before loading it with dynamic data. I have a LinearLayout with a empty List, wen the appropriate menu is selected I load it with dynamic data which takes some time to load, but wat happens is the screen is empty till the layout is filled with data. I need to display the empty layout which has ly title and show a ProgreesDialog till the list is filled with data. Here is my Activity class.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deals);
loadDeals();//fills the list with data(URL call)
}
You can user AsyncTask for loading data. During which you will be able to show ProgressDialog as well
http://developer.android.com/reference/android/os/AsyncTask.html
Use AsynchronousTask Class and call loadDeals() from doInBackground Method.Just capture the data on that method.Then work ur UI in postExecute method.It will solve your problem

Android tabs - avoid recreating activities on tab clicks

I've put a little app together that has three tabs to show three different web pages. It does work however I am bit worried I haven't got enough control over how this whole thing works. When I click a tab, I get a web page loaded (see code sample below), now when I click another tab another page loads in another view. When I go back to the first tab, the whole thing get initilized again and the page loads. Is there a way how I can control this and keep the underneeth tab's activity in its current state as long as I want (and say only "refresh" the page when it changes).
do I need to handle onPause()/onResume() methods for that or instead implement my tabs as views of a single activity (is this possible at all?)? How do I store the state of my activity to avoid re-initializing it every time?
this how activities are hooked to tabs:
intent = new Intent().setClass(this, tab_schedule.class);
spec = tabHost.newTabSpec("Schedule").setIndicator("Schedule",
res.getDrawable(R.drawable.tab_icon_schedule)).setContent(
intent);
tabHost.addTab(spec);
the tab_schedule.class does a simple web page load:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_people);
try {
WebView currentView = (WebView) findViewById(R.id.tab_people_WebView);
currentView.getSettings().setJavaScriptEnabled(true);
currentView.loadUrl("http://pda.lenta.ru");
} catch (Exception e) {
Log.v("webClientInit", e.getMessage());
}
}
If you don't want to create a new activity for each tab, you can use a TabWidget with a FrameLayout to toggle between views.
As to switching activities, see this question for a way to not recreate the activity each time.
Regardless, you should always implement onPause and onResume to restore the state of your app. You might want to read up on the Activity Lifecycle, but basically you cannot prevent your activity from being killed if goes into the background. Thus, you should store your state in onPause. The link above has some info on how to do so as well.
To bring the previous activity to the top of the stack use intent.addFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

Categories

Resources