Created
June 25, 2015 04:52
-
-
Save ralphpina/11960241d159f6a72ab5 to your computer and use it in GitHub Desktop.
Problematic class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// what package should Fragment belong to? I want to use it in Gingerbread! | |
public class MyFragment extends Fragment { | |
private TextView mTextView; | |
// Holding references to Activities is a no no. | |
// You are leaking its whole view hierarchy | |
private MyCallBack mMyCallBack; | |
// In fragments inflating views happens on onCreateView | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.fragment_my); | |
mTextView = (TextView) findViewById(R.id.textview); | |
} | |
// Activity will be null the first time this method is called. | |
// we should interact with the Activity in onResume() or onActivityCreated() | |
@Override | |
public void onAttach(Activity activity) { | |
super.onAttach(activity); | |
mMyCallBack = (MyCallBack) activity; | |
mMyCallBack.onAttach(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mTextView.setText("Welcome to your fragment."); | |
// We should not do file I/O here | |
if (!checkDatabaseForSyncResource()) { | |
new MyAsyncTask().execute("resourceA"); | |
} else { | |
mMyCallBack.resourceLoaded(); | |
} | |
} | |
// what happens if the Activity and Fragment are no longer around when this returns? | |
private class MyAsyncTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
} | |
@Override | |
protected String doInBackground(String... params) { | |
mTextView.setText("Started downloading something"); | |
return goGetATokenFromServer(params); | |
} | |
@Override | |
protected void onProgressUpdate(Void... values) { | |
super.onProgressUpdate(values); | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
mMyCallBack.setToken(s); | |
persistTokenDbSync(s); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment