public class MyApplicationClass extends Application {@Overridepublic void onCreate() {super.onCreate();// TODO Put your application initialization code here.}}
//used in onCreate() and onConfigurationChanged() to set up the UI elementspublic void InitializeUI(){//get views from ID'sthis.textViewHeaderMainMessage = (TextView) this.findViewById(R.id.TextViewHeaderMainMessage);
//etc... hook up click listeners, whatever you need from the Views}
//Called when the activity is first created.@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);
InitializeUI();}
//this is called when the screen rotates.// (onCreate is no longer called when screen rotates due to manifest, see: android:configChanges)@Overridepublic void onConfigurationChanged(Configuration newConfig){super.onConfigurationChanged(newConfig);setContentView(R.layout.main);
InitializeUI();}
onConfigurationChanged is called when the screen rotates.(onCreate is no longer called when the screen rotates due to manifest, see:android:configChanges)
import android.content.res.Configuration;import android.os.Bundle;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Fragment mFragment;
private int mSelected = -1;
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.d(TAG, "onCreate " + "");
// null check not realy needed - but just in case...if (savedInstanceState == null) {
initUi();
// get an instance of FragmentTransaction from your ActivityFragmentManager fragmentManager = getSupportFragmentManager();FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
/*IMPORTANT: Do the INITIAL(!) transaction only once!* If we call this everytime the layout changes orientation,* we will end with a messy, half-working UI.* */mFragment = FragmentOne.newInstance(mSelected = 0);fragmentTransaction.add(R.id.frame, mFragment);fragmentTransaction.commit();}}
@Overridepublic void onConfigurationChanged(Configuration newConfig) {super.onConfigurationChanged(newConfig);Log.d(TAG, "onConfigurationChanged " +(newConfig.orientation== Configuration.ORIENTATION_LANDSCAPE? "landscape" : "portrait"));
initUi();
Log.i(TAG, "onConfigurationChanged - last selected: " + mSelected);makeFragmentTransaction(mSelected);}
/*** Called from {@link #onCreate} and {@link #onConfigurationChanged}*/private void initUi() {setContentView(R.layout.activity_main);Log.d(TAG, "onCreate instanceState == null / reinitializing..." + "");Button btnFragmentOne = (Button) findViewById(R.id.btn_fragment_one);Button btnFragmentTwo = (Button) findViewById(R.id.btn_fragment_two);btnFragmentOne.setOnClickListener(this);btnFragmentTwo.setOnClickListener(this);}
/*** Not invoked (just for testing)...*/@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);Log.d(TAG, "onSaveInstanceState " + "YOU WON'T SEE ME!!!");}
/*** Not invoked (just for testing)...*/@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);Log.d(TAG, "onSaveInstanceState " + "YOU WON'T SEE ME, AS WELL!!!");}
@Overrideprotected void onResume() {super.onResume();Log.d(TAG, "onResume " + "");}
@Overrideprotected void onPause() {super.onPause();Log.d(TAG, "onPause " + "");}
@Overrideprotected void onDestroy() {super.onDestroy();Log.d(TAG, "onDestroy " + "");}
@Overridepublic void onClick(View v) {
switch (v.getId()) {case R.id.btn_fragment_one:Log.d(TAG, "onClick btn_fragment_one " + "");makeFragmentTransaction(0);break;
case R.id.btn_fragment_two:Log.d(TAG, "onClick btn_fragment_two " + "");makeFragmentTransaction(1);break;
default:Log.d(TAG, "onClick null - wtf?!" + "");}}
/*** We replace the current Fragment with the selected one.* Note: It's called from {@link #onConfigurationChanged} as well.*/private void makeFragmentTransaction(int selection) {
switch (selection) {case 0:mFragment = FragmentOne.newInstance(mSelected = 0);break;case 1:mFragment = FragmentTwo.newInstance(mSelected = 1);break;}
// Create new transactionFragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,// and add the transaction to the back stacktransaction.replace(R.id.frame, mFragment);
/*This would add the Fragment to the backstack...* But right now we comment it out.*/// transaction.addToBackStack(null);
// Commit the transactiontransaction.commit();}
}
和样本片段:
import android.os.Bundle;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;
/*** @author Martin Pfeffer (pepperonas)*/public class FragmentOne extends Fragment {
private static final String TAG = "FragmentOne";
public static Fragment newInstance(int i) {Fragment fragment = new FragmentOne();Bundle args = new Bundle();args.putInt("the_id", i);fragment.setArguments(args);return fragment;}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {Log.d(TAG, "onCreateView " + "");return inflater.inflate(R.layout.fragment_one, container, false);}
}
@Overridepublic void onSaveInstanceState(Bundle outState) {/*Save your data to be restored hereExample: outState.putLong("time_state", time); , time is a long variable*/super.onSaveInstanceState(outState);}
然后使用bundle恢复状态。
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
if(savedInstanceState!= null){/*When rotation occursExample : time = savedInstanceState.getLong("time_state", 0); */} else {//When onCreate is called for the first time}}
android:configChanges="keyboardHidden|orientation|screenSize"
@Overridepublic void onConfigurationChanged(Configuration config) {super.onConfigurationChanged(config);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {//Handle rotation from landscape to portrait mode here} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){//Handle rotation from portrait to landscape mode here}}
@Overridepublic void onConfigurationChanged(Configuration config) {super.onConfigurationChanged(config);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {//Handle rotation from landscape to portrait mode here} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){//Handle rotation from portrait to landscape mode here}}
if(savedInstanceState!= null){/*When rotation occursExample : time = savedInstanceState.getLong("time_state", 0); */} else {//When onCreate is called for the first time}