I'm having a hard time getting the desired performance while loading a video in a Fragment using AsyncTask.
I think Im tying up the UI thread, but I don't understand where, and I don't see a memory leak.
I have tried executing the AyncTask
from onAttach()
, OnCreate()
, onCreateView()
, and setUserVisibleHint()
I have also tried setting and using onPreparedListener()
In all these attempts I'm still seeing a performance lag when the VideoFragment
lifecycle gets called. By lag I don't mean connectivity since Im loading the video from the raw
folder.
So, as it transitions from IntroFragment
to DetailsFragment
, VideoFragment
's lifecycle (onAttach,onCreate,onCreateView, etc) fires off, and this is where I see the "lag" right before DetailsFragment
becomes visible.
FragmentsHolder
public abstract class FragmentsHolder extends FragmentActivity{
@Override
final protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
mPagerAdapter = new PagerIntroAdapter(super.getSupportFragmentManager(), fragments);
pager = (ViewPager) findViewById(R.id.view_pager);
pager.setAdapter(this.mPagerAdapter);
}
}
FirstSequence
public class FirstSequence extends FragmentsHolder {
@Override
public void init(Bundle savedInstanceState) {
addSlide(IntroFragment.newInstance("First String","Second String",
R.drawable.intro_fragment_drawable, 0, 0, 0, Color.parseColor("#4798af"), 1));
addSlide(DetailsFragment.newInstance("First Details String","Second Details String",
R.drawable.details_fragment_drawable, 0, 0, 0, Color.parseColor("#4798af"), 1));
addSlide(FourImagesFragment.newInstance("Title String","Instruction String", R.drawable.image_1, R.drawable.image_2, R.drawable.image_3,
R.drawable.image_4, Color.parseColor("#4798af"), 1));
addSlide(VideoFragment.newInstance(R.raw.my_video,"Video title string","Video subtitle string",
0, 0, 0, 0, Color.parseColor("#4798af"), 1));
VideoFragment
public class VideoFragment extends Fragment{
View hackyView;
private Handler subt_text;
private static final String ARG_VIDEO_TO_LOAD = "videotoload";
...
public static VideoFragment newInstance(int videoToLoad, String title, String subTitle,int imageDrawable1,int imageDrawable2,int imageDrawable3,int imageDrawable4,int bgColor, int progressCounter){
VideoFragment sampleSlide = new VideoFragment();
Bundle args = new Bundle();
args.putInt(ARG_VIDEO_TO_LOAD,videoToLoad);
...
return sampleSlide;
}
private int videoToLoad, ....;
private String title, ...;
@Override
public void onAttach(Context context) {
super.onAttach(context);
System.out.println("VideoFragment OnAttach ");
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//get args
if (getArguments() != null && getArguments().size() != 0) {
videoToLoad = getArguments().getInt(ARG_VIDEO_TO_LOAD);
...
}
System.out.println("VideoFragment OnCreate ");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_video, container, false);
hackyView = v;
//new loadVideoTask().execute();
System.out.println("VideoFragment OnCreateView ");
return vView;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
new loadVideoTask().execute();
}else {
}
}
private class loadVideoTask extends AsyncTask<Object, Object, Object> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
System.out.println("AsyncTask load onPreExecute ");
DialogMessage = "Loading Video";
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage(DialogMessage);
pDialog.show();
}
@Override
protected Object doInBackground(Object... params) {
slideUpAnim = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up);
cv1 = (CardView) hackyView.findViewById(R.id.cv1);
//Initialize the rest of the Views, animations, etc
...
return hackyView;
}
protected void onPostExecute(Object result) {
//process result load video
loadVideo();
pDialog.dismiss();
}
}
private void loadVideo() {
subt_text = new Handler();
vid.setVideoURI(Uri.parse(urlpath));
vid.requestFocus();
vid.seekTo(seekto);
//set all views
}
}
I've read numerous questions on fragments, their life cycles, etc, but I still can't figure out how do I use those answers to help me understand what Im doing wrong in my implementation.