Search in sources :

Example 1 with OnCardClickListener

use of com.example.android.batchstepsensor.cardstream.OnCardClickListener in project Android-Developers-Samples by johnjohndoe.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    FragmentManager fm = getSupportFragmentManager();
    BatchStepSensorFragment fragment = (BatchStepSensorFragment) fm.findFragmentByTag(FRAGTAG);
    if (fragment == null) {
        FragmentTransaction transaction = fm.beginTransaction();
        fragment = new BatchStepSensorFragment();
        transaction.add(fragment, FRAGTAG);
        transaction.commit();
    }
    // Use fragment as click listener for cards, but must implement correct interface
    if (!(fragment instanceof OnCardClickListener)) {
        throw new ClassCastException("BatchStepSensorFragment must " + "implement OnCardClickListener interface.");
    }
    OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);
    mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
    if (mRetentionFragment == null) {
        mRetentionFragment = new StreamRetentionFragment();
        fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
    } else {
        // If the retention fragment already existed, we need to pull some state.
        // pull state out
        CardStreamState state = mRetentionFragment.getCardStream();
        // dump it in CardStreamFragment.
        mCardStreamFragment = (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
        mCardStreamFragment.restoreState(state, clickListener);
    }
}
Also used : FragmentManager(android.support.v4.app.FragmentManager) FragmentTransaction(android.support.v4.app.FragmentTransaction) CardStreamState(com.example.android.batchstepsensor.cardstream.CardStreamState) StreamRetentionFragment(com.example.android.batchstepsensor.cardstream.StreamRetentionFragment) OnCardClickListener(com.example.android.batchstepsensor.cardstream.OnCardClickListener)

Aggregations

FragmentManager (android.support.v4.app.FragmentManager)1 FragmentTransaction (android.support.v4.app.FragmentTransaction)1 CardStreamState (com.example.android.batchstepsensor.cardstream.CardStreamState)1 OnCardClickListener (com.example.android.batchstepsensor.cardstream.OnCardClickListener)1 StreamRetentionFragment (com.example.android.batchstepsensor.cardstream.StreamRetentionFragment)1