加载中...
The Wayback Machine - https://sup1a9wrlpyh5li9ro.vcoronado.top/web/20140305055300/http://developer.android.com:80/samples/BatchStepSensor/src/com.example.android.batchstepsensor/MainActivity.html
to top
BatchStepSensor / src / com.example.android.batchstepsensor /

MainActivity.java

1
/*
2
* Copyright 2013 The Android Open Source Project
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
*     http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
 
17
 
18
 
19
package com.example.android.batchstepsensor;
20
 
21
import android.os.Bundle;
22
import android.support.v4.app.FragmentManager;
23
import android.support.v4.app.FragmentTransaction;
24
import android.view.Menu;
25
 
26
import com.example.android.common.activities.SampleActivityBase;
27
import com.example.android.common.logger.Log;
28
 
29
public class MainActivity extends SampleActivityBase implements CardStream {
30
    public static final String TAG = "MainActivity";
31
    public static final String FRAGTAG = "BatchStepSensorFragment";
32
 
33
    private CardStreamFragment mCardStreamFragment;
34
 
35
    private StreamRetentionFragment mRetentionFragment;
36
    private static final String RETENTION_TAG = "retention";
37
 
38
    @Override
39
    protected void onCreate(Bundle savedInstanceState) {
40
        super.onCreate(savedInstanceState);
41
        setContentView(R.layout.activity_main);
42
 
43
        FragmentManager fm = getSupportFragmentManager();
44
        BatchStepSensorFragment fragment =
45
                (BatchStepSensorFragment) fm.findFragmentByTag(FRAGTAG);
46
 
47
        if (fragment == null) {
48
            FragmentTransaction transaction = fm.beginTransaction();
49
            fragment = new BatchStepSensorFragment();
50
            transaction.add(fragment, FRAGTAG);
51
            transaction.commit();
52
        }
53
 
54
        // Use fragment as click listener for cards, but must implement correct interface
55
        if(!(fragment instanceof OnCardClickListener)){
56
            throw new ClassCastException("BatchStepSensorFragment must " +
57
                    "implement OnCardClickListener interface.");
58
        }
59
        OnCardClickListener clickListener = (OnCardClickListener) fm.findFragmentByTag(FRAGTAG);
60
 
61
        mRetentionFragment = (StreamRetentionFragment) fm.findFragmentByTag(RETENTION_TAG);
62
        if (mRetentionFragment == null) {
63
            mRetentionFragment = new StreamRetentionFragment();
64
            fm.beginTransaction().add(mRetentionFragment, RETENTION_TAG).commit();
65
        } else {
66
            // If the retention fragment already existed, we need to pull some state.
67
            // pull state out
68
            CardStreamState state = mRetentionFragment.getCardStream();
69
 
70
            // dump it in CardStreamFragment.
71
            mCardStreamFragment =
72
                    (CardStreamFragment) fm.findFragmentById(R.id.fragment_cardstream);
73
            mCardStreamFragment.restoreState(state, clickListener);
74
        }
75
    }
76
 
77
    public CardStreamFragment getCardStream() {
78
        if (mCardStreamFragment == null) {
79
            mCardStreamFragment = (CardStreamFragment)
80
                    getSupportFragmentManager().findFragmentById(R.id.fragment_cardstream);
81
        }
82
        return mCardStreamFragment;
83
    }
84
 
85
    @Override
86
    protected void onSaveInstanceState(Bundle outState) {
87
        super.onSaveInstanceState(outState);
88
        CardStreamState state = getCardStream().dumpState();
89
        mRetentionFragment.storeCardStream(state);
90
    }
91
}