I am displaying search results from a webservice.
What I do is OnCreate I hit the webservice display records, as android supports multitasking. If user opens another screen and after some time comes back to the search results page, the application starts acting crazy....
OnCreate method I load data some thing like :
private void loadData() throws Throwable{
try {
jsonArray = JSONService.getJsonArray(getResources().getString(R.string.catJson));
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
throw e;
}
}
and then I iterate through json array and change labels value to display result on the screen.
Any ideas how to fix this?
Yes, please explain our problem better. Are you familiar with the Activity life-cycle? There are several different callbacks that you must manage in order for your concept of multitasking to actually work. Based on the limited info you've provided, I don't think you're saving any of your application state when your Activity loses focus. So your process may be getting shutdown and when you come back, your JSON array is gone. Read this:
http://developer.android.com/guide/topics/fundamentals.html#lcycles
Are you calling the super in your OnCreate?
super.onCreate(savedInstanceState);
Related
My Android app has this flow of screens when launched:
Splash -> Chat Groups > Chat Screen (showing chat messages)
On Chat Screen I have my Custom RecyclerView Implemented.
On fresh launch (or after killing the app), I go to Chat Screen, it loads previous messages fine, and new incoming message is also seen when u r on this screen.
Now if I press Android's Back button few time to exit the app, and then relaunch the app and go to Chat Screen, previous messages appear fine BUT the new incoming message is not visible.
Important thing is, even if I don't go to Chat Screen the first time and close the app from Groups Screen, then relaunching and going to Chat Screen again causes the problem and I dont see new incoming Chat messages.
I have debugged it and all code is being executed fine. The incoming message is added to the list of RecycleView, and notifyDataSetChange() is being called, but onBindViewHolder() is not being called in this case, and that's why the list doesn't get updated.
The code is pretty lengthy, but if u still need to see it then I'll try to add.
This is driving me crazy, I am pretty sure it's a bug in Android.
If u can propose a workaround, like clearing the RecyclerView or Adapter somehow that it gets to same state as when i Kill the app and launch..
Here is the code:
//Initialize Recycler view
mMessageRecycler = findViewById(R.id.recyclerview_message_list)
mMessageRecycler?.layoutManager = LinearLayoutManager(this)
....
if (messagesAdapter == null) {
messagesAdapter = NewMessageListAdapter(this)
mMessageRecycler?.adapter = messagesAdapter
}
//Adapter
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position:Int) {
val message = messageList[position]
(holder as ReceivedMessageHolder).bind(message)
}
override fun getItemCount(): Int {
return messageList.size
}
...
//add new chat message. Breakpoint does hit this code
messageList.add(newMessage)
notifyDataSetChanged()
As it's impossible to tell what is going on without looking at a bigger picture, I'll give you a few pointers.
a. I suggest you attempt to use ListAdapter<T, K> as it forces you to provide a DiffUtil.ItemCallback implementation. This will allow you to avoid calling the expensive and extremely inefficient notifyDataSetChanged(); instead you will call adapter.submitList(...) and supply a List<T> with your data.
K is the ViewHolder type. Usually you use RecyclerView.ViewHolder (if I correctly recall or if you only have one viewHolder type then you can couple that there and use it directly. Otherwise you'll just have to "cast" your ViewHolders to be able to call their "bind" method.
b. As for "it doesn't work when I get back", this is a bit harder to detect, as we haven't seen how/where/when you fetch this data; are you using Android Coroutines? Are the list of messages stored in a repository relying on memory or database persistence? who updates this list?
As you can see, there are a few outstanding questions that we (the readers) cannot possibly infer given the information you've provided.
If you want to see the simplest example of a RecyclerView with ListAdapter, I often tend to link this sample I wrote because it shows how to put all pieces together.
c. You shouldn't need to do if (adapter == null) { // create it and set it } either. You can have:
class YourActivityOrFragment : ... {
private val adapter = YourAdapter()
override fun onViewCreated(...) {
yourRecyclerView.adapter = adapter
}
You can later set the data in the adapter once you have it, there's no need to delay the creation of this. If you're going to use a LinearLayoutManager, remember you can also set it directly in XML and avoid writing the code.
Finally I found the problem! It was due to some memory leak and threads issue.
At some point in the code i was re-initializing my Mqtt class, without checking if it is not null. So I just added a null check and it fixed
if (mqttMy == null) // added
mqttMy = MqttMy(context)
I'm an early and very happy adopter of both Flux and React so much so that, recently, I ported Fluxxor into Android and it's been okay so far.
The issue I am having with it Flux is dealing with data for a Single Item or Details Page. Bear with me below. I will try to be as clear as I can.
The pattern I am using is.
On page load(componentWillMount/componentWillReceiveProps and onStart), I check if the id passed to the page (via url or bundle) matches the id of the item currently in the store and the page if the store is in a processing or success state.
If yes, I do nothing, else, I dispatch an action to load the data for that item.
componentWillMount: function () {
id = this.props.params.path.split("-")[0];
var artistData = this.props.state.artistData;
if(artistData.id != id)
this.getFlux().actions.artistActions.loadArtist(id);
else if (!artistData.artist && !artistData.loading)
this.getFlux().actions.artistActions.loadArtist(id);
this.getFlux().actions.userActions.fetchSuggestions();
}
protected void onStart() {
GenreSongsStore.State state = App.getFlux().getStore(GenreSongsStore.class).getState();
if(mId == state.Genre.getId()) {
if (state.HasMore)
App.getFlux().getActions().Genres.songs(mId, state.Page + 1);
}
else
App.getFlux().getActions().Genres.songs(mId, 1);
super.onStart();
}
In React this is fine since you use a single state on the root. I didn't bother too much until I started working with Android.
Here, I don't use a single state but query the relevant store and it totally smells
If you are not using that page, the data is still held in memory
Since the data is not shared it seems there is no benefit to doing it like this
Won't it simply be easier to load the data in the component/activity/fragment?
However, I get the benefit of maintaining the currently loading state. So the user can minimize and reopen the app and we continue (no need for saving an instance bundle).
I know by doing it like this, I lose the benefit of unidirectional data flow. But it seems to make more sense in the context of Android (pun intended).
Can I have your views on how you do this and if I'm simply worried about nothing.
NB: The data is not shared by any other stores at.
I need to be able to cancel tasks involving web requests.
but I got some issues of memory management and exception handling when something fail.
For example:
I want to create a text edit for searching apps in user device.
so whenever user end a key, I want to clear current search task, and restart search.
The problem I got is in android 4.4, when trying to load the label of the app (to get it's name) I get an exception.
Also of I try to search contacts I'm getting an invlalid uri, fo rcontact photo.
I don't want any help dealing with this errors, I want a solution that will help me ignore this erros (try catach dont work) currently the entire ui get's stuck and app crash.
I'm using the executors service and call cancel when user gives me an input. but it's not enough.
Any advice will be appreciated, thanks.
if you're using AsyncTask i would like to suggest you to make it like this
AsyncTask<Void, Void, Void> a = new AsyncTask<Void, Void, Void>(){
#Override
protected Void doInBackground(Void... params) {
return null;
}
};
a.execute();
// at some point you want to cancel just use
a.cancel(true);
You can even put tasks together in List or array and loop to cancel it whenever you want.
You can try like this: what you want is getting result in background by network and show result in view ,you can Register a Listener to one task,. you can callback method of listener to use show result in view if it get the results in background ,if you do not need the result of current task and want satrt the next task ,you can only remove listener of current task,so the view will not reload
This is my idea ,I am so sorry for my bad english , if you do not see ,you can send message to me, and my email is bjltxsp#gmail.com
I'd like to ask the logic for first asking the user to fill up a profile form so it is filled, and then directed to the main activity. After closing the app and then re open it again, the profile activity should not be first launched but now the main activity. How can I do this? I'm in need of help. Thanks.
This is what I have tried so far:
private void doThread(){
Thread timer = new Thread(){
public void run(){
try{
sleep(5000); // sleeps/delays for 3 seconds
} // end try
catch(InterruptedException e){
e.printStackTrace();
}finally{
// this is going to create new intent activity for
// based on the action name (com.fps.ihealthfirst.IHEALTHFIRSACTIVITY)
boolean firstTime = mPreferences.getBoolean("user_prefs", true);
if (firstTime) {
Intent myProfile = new Intent( Disclaimer.this, Profile_Pref.class );
startActivity(myProfile);
}
else{
Intent openIHealthFirst = new Intent( "com.fps.iHealthFirst.IHEALTHFIRSTACTIVITY" );
startActivity( openIHealthFirst );
finish();
}
}// end finally
} // end run method
}; // end thread
timer.start();
}
Depending on your choice, you can choose to save the information collected from the Profile Form in either a Database or a SharedPreferences file. This part is rather subjective and if you are already employing a Database in your application, you might consider it.
Here are a few suggestions on handling the logic / flow after the user has setup / entered his Profile details:
First: If you aren't already employing a Splash Screen, you must consider creating one. This will give you a small buffer time to check if the user has already entered his profile details.
Second: If he hasn't, you can open the Profile Form Activity. If, after checking either of the Database or SharedPreferences file, you find data indicating a filled form, you can display the main activity directly.
I personally, would be more inclined towards using SharedPreferences for this task.
Here are a few tutorials to get started with SharedPreferences:
http://android-er.blogspot.in/2011/01/example-of-using-sharedpreferencesedito.html
http://saigeethamn.blogspot.in/2009/10/shared-preferences-android-developer.html
http://myandroidsolutions.blogspot.in/2012/06/android-store-persistent-data-using.html
http://moorandroid.blogspot.in/p/shared-preferences-on-android.html
They may not be specific to your question, but will give you the logic to save values. Retrieving the saved values would be fairly simple.
Hope any of this helps.
One way is to save the form information on to SD card, then load and check for the information, if the information is present there, then you can move to next activity. Check this answer for explanation to it.
Can I have an android activity run only on the first time an application is opened?
The other is to check for a specific shared preference in the main activity, if that shared preference is missing, then launch the single run activity again. Check the following answer for an explanation to it.
How to launch activity only once when app is opened for first time?
Determine if android app is the first time used
You can use SharedPreferences.I had this same question with a good answer here. Check it out.
In my app I have a header with icon hidden, I have a adapter with a listview when I click the listview I go to a login screen using listener, when the login is successful is should come back to listview(adapter) and icon should get visible on header.
In the login activity I have the following code:
public void onClick(View v) {
String password = etPassword.getText().toString();
if(password.equals("guest")){
SearchAdapter.setImgVisibility();
} else {
//-----
}
finish();
}
In my adapter I am calling the setImgVisibility() as follows, but it is not working
public static void setImgVisibility() {
img.setVisibility(View.VISIBLE);
}
I am getting a Nullpointerexception near the line img.setVisibility(View.VISIBLE);
I am stuck here and don't know what I am doing wrong. Any suggestions or help is appreciated
I would imagine that img is null. You need to look at where this value is set and make sure happens before you call the method setImgVisibility.
Show more of your complete code for people to help further.
Additionally, i've just noticed you've used a static reference to your search adapter, you should be really careful using statics, especially where any referencing of images is concerned as images can be bound to the context, as such unless you nullify the static you will end up with a memory leak. (this used to be an old problem, not sure its still valid, but i would still avoid using a static reference).
Without more code we're not likely to be able to properly help you. For example are you switching activities when logging in? If you are, this won't work at all.
[given the comment below] If you switch activities then your activity containing the list view is going to be destroyed and then rebuilt then you navigate back to it. or it will at least go through the activity lifecycle. This means you can set the icon during the instantiation of the header img.
You could store your logged in state as a property of the Application or a preference. Grab this value when you set the header image and set the image accordingly.
your img object is null. Is your img object is same as View v then you can pass v in setImgVisibility() and then set v.setVisibility(View.VISIBLE)