The relationship between TabActivity and inline activity

I am trying to figure out the best communication practice between TabActivity and the child action built into this TabActivity.

There is a button in my TabActivity. When the button is clicked, I want the child activity built into this TabActivity to be updated. I wrote the code as shown below and just wondering if this is good practice. Thank.

MyTabActivity.java

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;
    }
}

ChildActivity.java

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;
        }
}
+3
source share
5 answers

Seems beautiful. A few notes:
- I see no reason to sync.
- I would replace

ChildActivity.s_childActivity.changeUI();

with

if(ChildActivity.s_childActivity != null){
    ChildActivity.s_childActivity.changeUI();
}

or even

try{
    ChildActivity.s_childActivity.changeUI();
} catch(Exception e){
    //log
}

to ensure the safety of the paranoid. :)

+2
source

TabActivity ActivityGroup, :

  • getCurrentActivity()

. ChildActivity.

ChildActivity childActivity = (ChildActivity) getCurrentActivity();
  • getLocalActivityManager(). GetActivity (String)

, /, .

ChildActivity childActivity = (ChildActivity) getLocalActivityManager().getActivity("Tab 1");

onNewIntent (Intent) ChildActivity:

Intent intent = new Intent();
intent.putExtra("xyz", "whatever"); // or a serializable

ChildActivity childActivity = (ChildActivity) getLocalActivityManager().getActivity("Tab 1");
childActivity.onNewIntent(intent);

, !

+7

ChildActivity childActivity = (ChildActivity) getLocalActivityManager().getActivity("Tab 1");
childActivity.onNewIntent(intent);

. , ( !!!), :

Intent intent = new Intent(this, ChildActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra(AlbumBrowser.INTENT_EXTRA_FILTER, mediaTitle);
getLocalActivityManager().startActivity("activityIdHere", intent);
+2

, .

ChildActivity. ? , , . A TabActivity a ChildActivity. , ChildActivity TabActivity, :

 public class TabActivity {

    private ChildActivity child;

    //remember to initialize child in onCreate

    //then, call methods using child.changeUI();, for example

} 

, A) TabActivity ChildActivity, ( , ChildActivity) B) ChildActivity TabActivity... , , , ( , , , ) - , , .

, ChildActivity, (TabActivity). ChildActivity registerParent() ChildActivity:

public class ChildActivity ...{

private TabActivity parent;

public void registerParent(TabActivity newParent){
    if (newParent != null){
        parent = newParent;
    }
}
}

, TabActivity , parent.someMethod();

, parent child ; .

+1

getParent(), - .

, , tabHost:

public class LaunchPadActivity extends Activity implements OnClickListener {

private static final int ICON_PROFILE = 0;

private static final int ICON_SEARCH = 1;

private static final int ICON_MAP = 2;

private static final int FAVOURITES = 3;

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

    setContentView(R.layout.launchpad);

    GridView launchPad = (GridView) findViewById(R.id.launchpad);
    launchPad.setAdapter(new LaunchIconAdapter(this));
}

public class LaunchIconAdapter extends BaseAdapter {
    private Context mContext;

    // references to our images
    private Integer[] mThumbIds = { R.drawable.user, R.drawable.find,
            R.drawable.map, R.drawable.favourites, R.drawable.reviews,
            R.drawable.news, R.drawable.tutorial, R.drawable.info,
            R.drawable.options, };

    public String[] texts = { "Profile", "Search", "Map", "Favourites",
            "Reviews", "News", "Tutorial", "Info", "Options" };

    public LaunchIconAdapter(Context c) {
        mContext = c;
    }

    // Number of thumbs determines number of GridView items
    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        // Icon elements
        LinearLayout launchIcon;
        ImageView launchImage;
        TextView launchText;

        if (convertView == null) {
            launchIcon = (LinearLayout) ((LayoutInflater) mContext
                    .getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.launchicon, null);

        } else {
            launchIcon = (LinearLayout) convertView;
        }

        // Add ClickListener with metadata
        launchIcon.setTag(new Integer(position));
        launchIcon.setOnClickListener(LaunchPadActivity.this);

        // Get subviews
        launchImage = (ImageView) launchIcon
                .findViewById(R.id.launch_image);
        launchText = (TextView) launchIcon.findViewById(R.id.launch_text);

        // Configure subviews
        launchImage.setImageResource(mThumbIds[position]);
        launchText.setText(texts[position]);

        return launchIcon;
    }

}

@Override
public void onClick(View v) {
    int position = ((Integer) v.getTag()).intValue();
    switch (position) {
    case ICON_PROFILE:
        Toast.makeText(this, "Profile", Toast.LENGTH_LONG).show();
        break;

    case ICON_SEARCH:
        Toast.makeText(this, "Search", Toast.LENGTH_LONG).show();
        ((TabActivity) getParent()).getTabHost().setCurrentTab(1);
        break;

    case ICON_MAP:
        Toast.makeText(this, "Map", Toast.LENGTH_LONG).show();
        ((TabActivity) getParent()).getTabHost().setCurrentTab(2);
        break;

    case FAVOURITES:
        Toast.makeText(this, "Map", Toast.LENGTH_LONG).show();
        ((TabActivity) getParent()).getTabHost().setCurrentTab(3);
        break;

    }

}

}

.

+1
source

Source: https://habr.com/ru/post/1752112/


All Articles