Loading New Data with Back Button?

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Loading New Data with Back Button?

Postby android.developer27 » Sat Apr 11, 2009 4:14 pm

I have a message application so that on view 1 is displayed a list of messages and when you go to view 2 (subactivity) you can send a message. I want the program to work so that when you hit the android back button, you can go back to view 1 and see the list of messages including the new message. Right now only the old data is displayed, and I noticed that displayContent() is not called when going back which I assumed it would. Is there any way to specify that when the back button is hit, to call displayContent() again on the previous activity?
android.developer27
Freshman
Freshman
 
Posts: 2
Joined: Wed Apr 01, 2009 2:12 am

Top

Postby padde » Sun Apr 12, 2009 11:40 pm

Here an working example...

Main Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.test;
  3.  
  4.  
  5.  
  6. import java.util.Vector;
  7.  
  8.  
  9.  
  10. import android.app.ListActivity;
  11.  
  12. import android.content.Context;
  13.  
  14. import android.content.Intent;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.os.Handler;
  19.  
  20. import android.os.Message;
  21.  
  22. import android.view.View;
  23.  
  24. import android.view.ViewGroup;
  25.  
  26. import android.widget.BaseAdapter;
  27.  
  28. import android.widget.Button;
  29.  
  30. import android.widget.ListView;
  31.  
  32. import android.widget.TextView;
  33.  
  34. import de.test.sub.SubActivity;
  35.  
  36.  
  37.  
  38. public class Test extends ListActivity {
  39.  
  40.     private TestAdapter myAdapter;
  41.  
  42.     private ListView myListView;
  43.  
  44.     public static MyHandler handler;
  45.  
  46.    
  47.  
  48.     public void onCreate(Bundle savedInstanceState) {
  49.  
  50.         super.onCreate(savedInstanceState);
  51.  
  52.  
  53.  
  54.         myListView = getListView();
  55.  
  56.         handler = new MyHandler();
  57.  
  58.        
  59.  
  60.         Button addButton = new Button(this);
  61.  
  62.         addButton.setText("Add Item");
  63.  
  64.         addButton.setOnClickListener(new Button.OnClickListener() {
  65.  
  66.             public void onClick(View v) {
  67.  
  68.                 startActivity(new Intent(Test.this, SubActivity.class));
  69.  
  70.             }
  71.  
  72.         });
  73.  
  74.         myListView.addHeaderView(addButton);
  75.  
  76.        
  77.  
  78.         myAdapter = new TestAdapter(this);
  79.  
  80.         setListAdapter(myAdapter);
  81.  
  82.        
  83.  
  84.         myAdapter.addItem("not much");
  85.  
  86.     }
  87.  
  88.  
  89.  
  90.     public class MyHandler extends Handler {
  91.  
  92.         public void handleMessage(Message msg) {
  93.  
  94.             if(msg.what == 1) {
  95.  
  96.                 myAdapter.addItem((String) msg.obj);
  97.  
  98.             }
  99.  
  100.             super.handleMessage(msg);
  101.  
  102.         }
  103.  
  104.     }    
  105.  
  106.    
  107.  
  108.     private class TestAdapter extends BaseAdapter {
  109.  
  110.         private Context cntxt;
  111.  
  112.         private Vector<String> items;
  113.  
  114.        
  115.  
  116.         public TestAdapter(Context context) {
  117.  
  118.             cntxt = context;
  119.  
  120.             items = new Vector<String>();
  121.  
  122.         }
  123.  
  124.        
  125.  
  126.         public void addItem(String item){
  127.  
  128.             items.add(item);
  129.  
  130.             notifyDataSetChanged();
  131.  
  132.         }
  133.  
  134.         public int getCount() { return items.size(); }
  135.  
  136.         public Object getItem(int position) { return items.get(position); }
  137.  
  138.         public long getItemId(int position) { return position; }
  139.  
  140.  
  141.  
  142.         public View getView(int position, View convertView, ViewGroup parent) {
  143.  
  144.             TextView tv;
  145.  
  146.             if (convertView == null)
  147.  
  148.                 tv = new TextView(cntxt);
  149.  
  150.             else
  151.  
  152.                 tv = (TextView)convertView;
  153.  
  154.             tv.setText(items.get(position));
  155.  
  156.             return tv;
  157.  
  158.         }
  159.  
  160.     }
  161.  
  162. }
  163.  
  164.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Sub Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.test.sub;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.os.Message;
  11.  
  12. import android.widget.EditText;
  13.  
  14. import android.widget.LinearLayout;
  15.  
  16. import android.widget.LinearLayout.LayoutParams;
  17.  
  18. import de.test.Test;
  19.  
  20.  
  21.  
  22. public class SubActivity extends Activity {
  23.  
  24.     private EditText et;
  25.  
  26.    
  27.  
  28.     public void onCreate(Bundle savedInstanceState) {
  29.  
  30.         super.onCreate(savedInstanceState);
  31.  
  32.        
  33.  
  34.         LinearLayout lin = new LinearLayout(this);
  35.  
  36.         LayoutParams param = new LayoutParams(-1,-2);
  37.  
  38.         lin.setLayoutParams(param);
  39.  
  40.         et = new EditText(this);
  41.  
  42.         et.setSingleLine();
  43.  
  44.         et.setLayoutParams(param);
  45.  
  46.         lin.addView(et);
  47.  
  48.         setContentView(lin);
  49.  
  50.     }
  51.  
  52.    
  53.  
  54.     protected void onPause() {
  55.  
  56.         super.onPause();
  57.  
  58.         Message msg = Test.handler.obtainMessage();
  59.  
  60.         msg.what = 1;
  61.  
  62.         msg.obj = et.getText().toString();
  63.  
  64.         Test.handler.sendMessage(msg);
  65.  
  66.         finish();
  67.  
  68.     }
  69.  
  70. }
  71.  
  72.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


When the SubActivity is visible and you press the back button on the device/emulator onPause is called
where a message is generated with the current content of the edittext afterwards the message get send
to the messagehandler of the main activity.

Greets
Padde
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests