Note that there are some explanatory texts on larger screens.

plurals
  1. POCommunication between TabActivity and the embedded activity
    text
    copied!<p>I am trying to figure out the best practice of communication between a TabActivity and the child activity embedded in this TabActivity.</p> <p>In my TabActivity, there is a button. When the button is clicked, I want the child activity embedded in this TabActivity to be updated. I wrote the code like below, and just wonder whether it is a good practice. Thanks.</p> <p>MyTabActivity.java</p> <pre><code>public class MyTabActivity extends TabActivity implements OnClickListener { private TabHost m_tabHost; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.ff_tab_activity); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); m_tabHost = getTabHost(); TabHost.TabSpec spec; Intent intent; intent = new Intent().setClass(this, ChildActivity.class); spec = m_tabHost.newTabSpec("Tab 1"); spec.setContent(intent); tabView = (TextView) inflater.inflate(R.layout.tab_indicator, null); spec.setIndicator(tabView); m_tabHost.addTab(spec); m_tabHost.setCurrentTab(0); ImageView nextButtonIv = (ImageView) findViewById(R.id.next_button); nextButtonIv.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.next_button: synchronized (ChildActivity.class) { if (null != ChildActivity.s_childActivity) { ChildActivity.s_childActivity.changeUI(); } } break; } } </code></pre> <p>ChildActivity.java</p> <pre><code>public class ChildActivity extends Activity { public static ChildActivity s_childActivity; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); synchronized (MatchupsActivity.class) { s_childActivity = this; } setContentView(R.layout.child_activity); } public void changeUi() { code that changes UI } protected void onDestroy() { super.onDestroy(); synchronized (MatchupsActivity.class) { s_childActivity = null; } } </code></pre>
 

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