Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A gist that allows to check whether or not objects given through a Bundle are of the same instance. Used for an example on http://magicmicky.github.io/android_development/loose_coupling_using_otto/
package com.magicmicky.loosecoopling;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyActivity extends ActionBarActivity {
private final static String TAG="Activity";
private static final String ARG_SHOP = "shop";
private static final String ARG_FRG = "frg";
private Shop mShop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
if (savedInstanceState == null) {
this.mShop = new Shop("Test shop");
Log.d(TAG, "shop created: " + ((Object)mShop).toString());
getSupportFragmentManager().beginTransaction()
.add(R.id.container, BlankFragment.newInstance(mShop))
.commit();
} else {
this.mShop = (Shop) savedInstanceState.get(ARG_SHOP);
Log.d(TAG, "shop retrieved from instance state: " + ((Object) mShop).toString());
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "Saving shop: " + ((Object) mShop).toString());
outState.putParcelable(ARG_SHOP, mShop);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy()");
}
/*
* Fragment
*/
public static class BlankFragment extends Fragment {
private static final String ARG_SHOP = "shop";
private static final String TAG = "Fragment";
private Shop mShop;
public static BlankFragment newInstance(Shop s) {
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putParcelable(ARG_SHOP, s);
fragment.setArguments(args);
return fragment;
}
public BlankFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.mShop = (Shop) getArguments().get(ARG_SHOP);
Log.d(TAG, "shop given to fragment: " + ((Object) mShop).toString());
}
if(savedInstanceState!=null && savedInstanceState.containsKey(ARG_SHOP)) {
this.mShop = (Shop) savedInstanceState.get(ARG_SHOP);
Log.d(TAG, "shop retrieved from instance state: " + ((Object) mShop).toString());
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy()");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "Saving shop: " + ((Object) mShop).toString());
outState.putParcelable(ARG_SHOP, mShop);
}
}
/*
* Model
*/
public static class Shop implements Parcelable {
private final static String TAG="Shop";
private String name;
public Shop(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.name);
Log.v(TAG, "writeToParcel of " + ((Object)this).toString());
}
private Shop(Parcel in) {
this.name = in.readString();
Log.v(TAG, "Creating from parcel of " + ((Object)this).toString());
}
public static final Creator<Shop> CREATOR = new Creator<Shop>() {
public Shop createFromParcel(Parcel source) {
return new Shop(source);
}
public Shop[] newArray(int size) {
return new Shop[size];
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment