Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get object reference from other activity [android]
    text
    copied!<p>I have two major classes in my project. The first is for creating the connection between the client and the server. The second is for switching between activities.</p> <p>first:</p> <pre><code>public class MyActivity extends Activity{ private ListView mList; private ArrayList&lt;String&gt; arrayList; private MyCustomAdapter mAdapter; public TCPClient mTcpClient; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); boolean flag = getIntent().getBooleanExtra("flag",false); arrayList = new ArrayList&lt;String&gt;(); final EditText editText = (EditText) findViewById(R.id.editText); Button send = (Button)findViewById(R.id.send_button); Button menu = (Button)findViewById(R.id.button1); if (flag == true) { //relate the listView from java to the one created in xml mList = (ListView)findViewById(R.id.list); mAdapter = new MyCustomAdapter(this, arrayList); mList.setAdapter(mAdapter); new connectTask().execute(""); Intent myIntent = new Intent(MyActivity.this,Menu.class); startActivity(myIntent); } send.setOnClickListener(new View.OnClickListener() { // @Override public void onClick(View view) { String message = editText.getText().toString(); //clean the listView to 1 item if (message.equals("clean")) { arrayList.removeAll(arrayList); mList.removeAllViewsInLayout(); } //add the text in the arrayList arrayList.add("c: " + message); //sends the message to the server if (mTcpClient != null) { mTcpClient.sendMessage(message); } //refresh the list mAdapter.notifyDataSetChanged(); editText.setText(""); } }); //change Activity to live screen mode (live) menu.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent myIntent = new Intent(MyActivity.this, Menu.class); startActivity(myIntent); } }); } public class connectTask extends AsyncTask&lt;String,String,TCPClient&gt; { @Override protected TCPClient doInBackground(String... message) { //we create a TCPClient object and mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() { // @Override //print the message as an Item public void messageReceived(String message) { //this method calls the onProgressUpdate publishProgress(message); } }); mTcpClient.run(); return null; } @Override protected void onProgressUpdate(String... values) { super.onProgressUpdate(values); //in the arrayList we add the messaged received from server arrayList.add(values[0]); // notify the adapter that the data set has changed. This means that new message received // from server was added to the list mAdapter.notifyDataSetChanged(); } } </code></pre> <p>}</p> <p>the object <code>TCPClient mTcpClient</code> is the major factor in my app. I use it communicate with the server. In addition, even if I switch between activities it is still running properly so I still get info from server even though I am not in that activity.</p> <p>second:</p> <pre><code>public class Menu extends Activity { public MyActivity myActivity; public TCPClient mtcp; protected void onCreate(Bundle savedInstanceState, MyActivity myActivity) { super.onCreate(savedInstanceState); setContentView(R.layout.menu); ImageView action = (ImageView) findViewById(R.id.imageView1); action.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // here I would like to use mTcpClient object mentioned in the first class return false; } }); </code></pre> <p>}</p> <p>Basically what I need is a help on how to create in the second class reference to the object <code>mTcpClient</code> that is described in the first class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload