0

I'm trying to do a chat application with two activities. On the first plan is a short list containing the last sent message in each row. A second activity contain all conversation. I use socket.io and my problem is that, when I click back button and then I come back to my app notifyDataSetChanged() stops working. My app recevie a messages from dialog box from a website which a cooperates with android app. In console log I see that a messages from a website are received and onTaskComplete() method is called but a listview is not refreshing. I read that an adapter loses a reference to a listview after restart an activity but how to see in my code I create a new adapter in onResume() method. I don't understand what I do wrong? While if I will throw out beyond the condition "mSocket.on("message", onMessage);" then a listview is refresh after come back to app but a messages are multiple as many times as there were returns.

below is my code, please help!

MainActivity:

package com.example.seadog.fb_dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import io.socket.client.Socket;
import io.socket.emitter.Emitter;

public class MainActivity extends Activity implements MyListener {

    public ListView listView;
    public MyBaseAdapter adapter;

    public TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        /*
         * Get Socket.io Object
         */

        SocketIO socketIo = new SocketIO();

        Socket mSocket = socketIo.getSocket();  // get socket
        Integer id = socketIo.getId();          // get Website ID

        if(mSocket == null) {

            socketIo.Connection();
            mSocket = socketIo.getSocket();

            mSocket.on("message", onMessage);

        }

        listView = (ListView) findViewById(R.id.listView);


        /*
         * OnItemClickListener
         */

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent intent = new Intent(MainActivity.this, Conversation.class);
                intent.putExtra("item", position);
                startActivity(intent);

                TextView textView = (TextView) view.findViewById(R.id.descitem);
                textView.setTypeface(null, Typeface.NORMAL);

            }

        });

        textView = (TextView) findViewById(R.id.count);

    }

    private Emitter.Listener onMessage = new Emitter.Listener() {

            /*
             * Message Listener
             */

        @Override
        public void call(Object... args) {

            Boolean isset = false;

            try {

                JSONObject object = (JSONObject) args[0];


                String _id = object.getString("_id");
                String message = object.getString("message");

                JSONObject obj = new JSONObject();
                obj.put("direction", "fb-lt");
                obj.put("message", message);
                obj.put("date", "2017-05-29T12:15:49.245Z");


                for(int i = 0; i < arrayList.size(); i++){

                    ListData ld = (ListData) arrayList.get(i);
                    String id = ld.getId();

                    if(_id.equals(id)){

                        JSONArray Data = ld.getData();
                        Data.put(obj);
                        ld.setDescription(message);

                        arrayList.set(i, ld);

                        isset = true;

                        Log.d("LOG", message);
                    }

                }

                if(!isset) {

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(obj);

                    ListData ld = new ListData();
                    ld.set_id(_id);
                    ld.setID(1);
                    ld.setTitle("Klient:");
                    ld.setDescription(message);
                    ld.setData(jsonArray);

                    arrayList.add(ld);

                }

                onTaskComplete();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    };


    public void onResume() {

        super.onResume();

        ArrayList clone = (ArrayList) arrayList.clone();
        arrayList.clear();
        arrayList.addAll(clone);

        adapter = new MyBaseAdapter(this, arrayList);
        listView.setAdapter(adapter);

    }

    @Override
    public void onTaskComplete() {

        runOnUiThread(new Runnable() {

            @Override
            public void run () {
                Log.d("LOG:","refresh");
                adapter.notifyDataSetChanged();
            }

        });

    }

}

Interface MyListener:

package com.example.seadog.fb_dialog;

import java.util.ArrayList;

public interface MyListener {

    ArrayList arrayList = new ArrayList();

    void onTaskComplete();

}
SeaDog
  • 645
  • 1
  • 9
  • 32
  • you want refresh the list view when use come back to your app again? – Hamza Jun 20 '17 at 16:39
  • Yes, because when I'll receive a new message while the app is running in background a listview can't refresh. ArrayList is updated but listview not yet. So during come to foreground I want to refresh a listview. – SeaDog Jun 21 '17 at 09:29

1 Answers1

0

If I remember well, when you open a new activity in android, that's something separate from the previous one. When you press the back button, it just kill the activity you were in and show the previous activity.

Maybe you can try to do what you need when back is pressed in this :

@Override
public void onBackPressed()
{
    //Do your things here  
}

Hope I helped.

Hawkydoky
  • 194
  • 1
  • 15
  • You have right but when I press back button from first activity (hide app) and then I open app then my listview is not refreshing. It's stops working only when I click back button and then I come back but when I press Home button or cart view and then I come back it is works. – SeaDog Jun 20 '17 at 15:46
  • The app is still running in background and is not really killed. Try to kill it manually and then see if your listview is refreshed. Try to add finish(); in the onBackPressed() function. But you'll always need to reload everything at each start. I'm not an expert so I can be wrong :/ – Hawkydoky Jun 20 '17 at 15:52
  • I want my app running in background. It have to works like messenger/viber etc. When I receive a message the sound will play and I'll open my app again. In each a moment a listview have to refresh. – SeaDog Jun 20 '17 at 15:58
  • Look at this, maybe it will help you. You can refresh your listview in the onResume() function. https://stackoverflow.com/questions/4414171/how-to-detect-when-an-android-app-goes-to-the-background-and-come-back-to-the-fo – Hawkydoky Jun 20 '17 at 16:14
  • I used in my project onResume() method but isn't work. How can I create a new reference to arraylist? Maybe with it is a problem? Look... When I opening my app first time, it's works great. For example I send a messages from a website and the app receive these messages. Next when I press back button (the app is background) and then come back to the foreground in this moment onResume() method refreshs a listview but next when I try to send a messages from a website in console log I see the messages are received because onTaskComplete() method is called but a listview is not refresh. Why? – SeaDog Jun 21 '17 at 08:51