Thank you for paying attention :D Help me with this as you can pls. So here is what i wanna do. I have four fragments and one activity.I playing a live video in first fragment and it's working fine.But when i replace first fragment with another one, first fragment's video still playing in back ground. What wanna do is play that video in all my fragments but with different sizes and no interference with one another. How can i do that? Here is my code.
First Fragment's code
public class FTop2 extends Fragment {
String Tag = "FTop2";
CallbackManager callbackManager;
ShareDialog shareDialog;
ProgressDialog progressDialog;
public FTop2() {}
protected void facebookSDKInitialize() {
FacebookSdk.sdkInitialize(getContext());
callbackManager = CallbackManager.Factory.create();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
facebookSDKInitialize();
Twitter.initialize(getActivity());
}
private class HttpUrl extends AsyncTask<Integer, Void, ArrayList<LiveUrl>> {
@Override
protected ArrayList<LiveUrl> doInBackground(Integer... i) {
return XmlPrepareClient.getTickedId("cid0003","1","1");
}
@Override
protected void onPostExecute(ArrayList<LiveUrl> result) {
for (int i=0; i<result.size();i++) {
String txt = result.get(i).url;
Log.e("play url", Config.B64decodeStr(txt));
}
VideoView vid = (VideoView) getView().findViewById(R.id.videoView);
String vid_url = Config.B64decodeStr(result.get(0).url);
Uri vid_uri = Uri.parse(vid_url);
vid.setVideoURI(vid_uri);
vid.start();
MediaController vid_control = new MediaController(getContext());
vid_control.setAnchorView(vid);
vid.setMediaController(vid_control);
if (progressDialog.isShowing()) {
progressDialog.hide();
}
}
}
@Override
public View onCreateView(LayoutInflater i, ViewGroup c,Bundle s) {
Bundle bundle = this.getArguments();
final int channel= bundle.getInt("channel");
final String cat= bundle.getString("cat");
final String img= bundle.getString("img");
shareDialog = new ShareDialog(this);
View view = i.inflate(R.layout.f_top2, c, false);
progressDialog = Config.getProgressDialog(getContext());
if(Config.isNetworkAvailable(getContext())) {
if (!progressDialog.isShowing()) progressDialog.show();
new HttpUrl().execute();
}else {
Config.ToastNet(getContext());
}
ImageView btnBack = (ImageView) view.findViewById(R.id.btn_dismiss);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().popBackStack();
}
});
ImageView btnDetail = (ImageView) view.findViewById(R.id.btn_detail);
btnDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new FTop2_1()).addToBackStack(Tag).commit();
}
});
ImageView btnProgram = (ImageView) view.findViewById(R.id.btn_program);
btnProgram.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new FTop2_2()).addToBackStack(Tag).commit();
}
});
ImageView btnComment = (ImageView) view.findViewById(R.id.btn_comment);
btnComment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new FTop2_3()).addToBackStack(Tag).commit();
}
});
Second fragment's code
public class FTop2_1 extends Fragment {
LinearLayout line;
TextView txt;
ArrayList<TV> datas = new ArrayList<>();
ListView list;
ProgressDialog progressDialog;
boolean isTablet;
public FTop2_1() {}
@Override
public void onCreate(Bundle s) {
super.onCreate(s);
}
private class HttpTask extends AsyncTask<Integer, Void, ArrayList<TV>> {
@Override
protected ArrayList<TV> doInBackground(Integer... params) {
return PrepareClient.postTV(params[0]);
}
@Override
protected void onPostExecute(ArrayList<TV> result) {
datas = result;
Adapter listAdapter1 = new Adapter(getContext(), R.layout.f_top2_1_item, datas);
ViewGroup.LayoutParams layoutParams = list.getLayoutParams();
if(!isTablet)
layoutParams.height = 160*result.size();
list.setLayoutParams(layoutParams);
list.setAdapter(listAdapter1);
if (progressDialog.isShowing()) {
listAdapter1.notifyDataSetChanged();
progressDialog.hide();
}
}
}
private class HttpUrl extends AsyncTask<Integer, Void, ArrayList<LiveUrl>> {
@Override
protected ArrayList<LiveUrl> doInBackground(Integer... i) {
return XmlPrepareClient.getTickedId("cid0003","1","1");
}
@Override
protected void onPostExecute(ArrayList<LiveUrl> result) {
for (int i=0; i<result.size();i++) {
String txt = result.get(i).url;
Log.e("play url", Config.B64decodeStr(txt));
}
VideoView vid = (VideoView) getView().findViewById(R.id.videoView);
String vid_url = Config.B64decodeStr(result.get(0).url);
Uri vid_uri = Uri.parse(vid_url);
vid.setVideoURI(vid_uri);
vid.start();
MediaController vid_control = new MediaController(getActivity());
vid_control.setAnchorView(vid);
vid.setMediaController(vid_control);
if (progressDialog.isShowing()) {
progressDialog.hide();
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle s) {
View view = inflater.inflate(R.layout.f_top2_1, container, false);
Button btnBack = (Button) view.findViewById(R.id.back);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().popBackStack();
}
});
isTablet = getResources().getBoolean(R.bool.isTablet);
list = (ListView) view.findViewById(R.id.list);
if(!isTablet)
list.setEnabled(false);
progressDialog = Config.getProgressDialog(getContext());
if(Config.isNetworkAvailable(getContext())) {
if (!progressDialog.isShowing()) progressDialog.show();
new HttpTask().execute(1);
new HttpUrl().execute();
}else{
Config.ToastNet(getContext());
}
line = (LinearLayout) view.findViewById(R.id.line);
line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = line.getLayoutParams();
params.height = 300;
line.setLayoutParams(params);
txt = (TextView) view.findViewById(R.id.text_toggle);
ImageView toggle = (ImageView) view.findViewById(R.id.toggle);
toggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Integer.parseInt(String.valueOf(line.getHeight())) == 300) {
line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = line.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
line.setLayoutParams(params);
} else {
line.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
ViewGroup.LayoutParams params = line.getLayoutParams();
params.height = 300;
line.setLayoutParams(params);
}
}
});
return view;
}
public class Adapter extends ArrayAdapter<TV> {
private LayoutInflater inflater;
private ArrayList<TV> records;
public Adapter(Context context, int resource, ArrayList<TV> datas) {
super(context, resource, datas);
this.records = datas;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return records.size();
}
public TV getItem(int position) {
return records.get(position);
}
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
if(convertView == null)
convertView = inflater.inflate(R.layout.f_top2_1_item, parent, false);
TV result = records.get(position);
((TextView)convertView.findViewById(R.id.desc1)).setText(result.time);
((TextView)convertView.findViewById(R.id.desc2)).setText(result.subtitle);
ImageView img = (ImageView) convertView.findViewById(R.id.img2);
Picasso.with(getContext())
.load(result.icon)
.placeholder(R.drawable.no_image)
.error(R.drawable.no_image)
.into(img);
return convertView;
}
}
}
@Override public void onDetach() {
if(vid.isPlaying()) {
vid.stopPlayback();
} }