The is an fragment that display the video.
This fragment can either
1) open a new activity on click button
2) replace with another fragment by calling
fragmentManager.beginTransaction().replace(R.id.container, f).addToBackStack(tag).commit();
for the 1) case , I would like to call player.stopPlayBack() to stop the video playing at backing
And for the 2) case , I would like to call player.stopPlayBack() and player.release() to terminate the player
The problem is , what event I should call for the case 1) and 2)? I try using onPause or onStop but both of them seems has not fired.
How to fix it?
Thanks a lot for helping.
Updated:
Video fragment code
public class Video extends Fragment implements MediaPlayer.OnPreparedListener {
#Bind(R.id.player) EMVideoView player;
#Bind(R.id.full_screen) ImageView full_screen;
Context ctx;
MyApp app;
String video_url;
int intent_code = 5545;
int pos;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.video, container, false);
ButterKnife.bind(this, view);
Bundle bundle = this.getArguments();
video_url = bundle.getString("video_url");
String id = bundle.getString("id");
app = (MyApp) getActivity().getApplicationContext();
app.record_view(id);
Main m = (Main)getActivity();
m.toggle_upload_btn(false);
pos = 0;
full_screen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getActivity(), VideoFullScreen.class);
i.putExtra("video_url", video_url);
i.putExtra("time", (int) player.getCurrentPosition());
startActivityForResult(i, intent_code); //random intent number
}
});
return view;
}
#Override
public void onPrepared(MediaPlayer mp) {
player.seekTo(pos);
player.start();
}
#Override
public void onResume() {
super.onResume();
player.setOnPreparedListener(this);
player.setVideoURI(Uri.parse(video_url));
}
#Override
public void onStop() {
super.onStop();
player.stopPlayback();
//player.release();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == intent_code) {
if(resultCode == Activity.RESULT_OK){
pos = data.getIntExtra("time", 0);
}
}
}
When the fragment is added to the backstack, and then getting replaced or removed - it will go like this:
onPause() -> onSaveInstanceState() -> onStop() -> onDestroyView()
If the fragment is removed, or replaced without getting added to the back stack, then following happens:
onPause() -> onSaveInstanceState() -> onStop() -> onDestroyView() -> onDestroy() -> onDetach() -> Fragment is destroyed.
And when a activity starts another activity (source):
The order of lifecycle callbacks is well defined, particularly when
the two activities are in the same process and one is starting the
other. Here's the order of operations that occur when Activity A
starts Acivity B:
Activity A's onPause() method executes. Activity B's onCreate(),
onStart(), and onResume() methods execute in sequence. (Activity B now
has user focus.) Then, if Activity A is no longer visible on screen,
its onStop() method executes.
Because you need to call on your activity where your fragment is existing, to start a new activity.
Related
I have a MainActivity with some cards which have different names. onClick, the title is passed as an intent via the adapter to the secondActivity and displayed as the header. From there, I can go to other activities. If I come back from one of these other activities (via the back button created by establishing second activity as the parent activity) the header is gone. How do I keep the header that was originally passed on as an intent or should I go about this completely different?
I have tried using onResume() and onStart() in the secondActivity to reassign the intent from a global variable.
The adapter class, where the card onClick method is written:
#Override
public void onBindViewHolder(final TripHolder tripHolder, final int position) {
Trip trip = trips.get(position);
tripHolder.setDetails(trip);
tripHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SecondActivity.class);
TextView card_title = v.findViewById(R.id.TripNamecl);
intent.putExtra("card_title", card_title.getText().toString());
context.startActivity(intent);
}
});
}
The secondActivity where the header should be displayed:
public class SecondActivity extends AppCompatActivity {
String name;
TextView header;
static final String STATE_HEADER = "header";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
name = getIntent().getStringExtra("card_title");
header = findViewById(R.id.TripsHeader);
header.setText(name);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(STATE_HEADER, name);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
name = savedInstanceState.getString(STATE_HEADER);
header.setText(name);
}
public void launchMapsActivity(View view) {
Uri gmmIntentUri = Uri.parse("geo:48.8566°,2.3522");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
}
public void launchTravelActivity(View view) {
Intent intent = new Intent(this, TravelActivity.class);
startActivity(intent);
}
public void launchPlansActivity(View view) {
Intent intent = new Intent(this, PlansActivity.class);
startActivity(intent);
}
}
SOLUTION:
The solution was to put android:launchMode="singleTop" into the manifest file for the secondactivity. It's described in more detail here: How can I return to a parent activity correctly?
In order to do it you should override onSavedInstanceState in your SecondActivity.
You can use something like that, obv adapt it to your needs:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//here you can save your variables
savedInstanceState.putString("myHeader", name);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//here you can retrieve your variables
name = savedInstanceState.getString("myHeader");
}
Let me know if this worked! good luck
remove header.setText(name); from onResume and onStart methods
If you want to save data in first activity between lifecicle method calls you have to save your data in Bundle object.
Override method onSaveInstanceState(Bundle bundle) of your activity:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("AStringKey", variableData);
outState.putString("AStringKey2", variableData2);
}
And also override Activity method onRestoreInstanceState(Bundle savedInstanceState):
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
variableData = savedInstanceState.getInt("AStringKey");
variableData2 = savedInstanceState.getString("AStringKey2");
setYourHeaderMethodExample(variableData2)
}
When coming back from third activity to second activity onStart() and onResume() method call executes. Try not using onStart() and onResume() for setting the header and use onCreate method for setting the header.
onCreate() method executes only once in its lifetime, but onStart() and onResume() will execute whenever the activity comes to screen. So when coming back from third activity we don't have any values in the intent.
In your second activity's onCreate method add:
getSupportActionBar().setHomeButtonEnabled(true);
and override the onOptionsItemSelected() method in the class and add the following code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
You don't need to override the onStart() and onResume() methods.
With the following code I can click a button to send a string from one fragment (Fragment A) into a MainActivity. How would I retrieve the string from the MainActivity and display it on a fragment B all in one go? I would like the fragments to act synchronized; to have Fragment B update itself as soon as I click the button in Fragment A. I can't seem to find anything in SO on this.
Interface:
public interface OnDataListener {
public void onDataReceived(String data);
}
Fragment A data listener:
OnDataListener mOnDataListener;
#Override
public void onAttach(Context context) {
try{
mOnDataListener = (OnDataListener) context;
Log.d(TAG, "onAttach was called" + context);
}catch (ClassCastException e){
Log.e(TAG, "onAttach: ClassCastException: " + e.getMessage() );
}
super.onAttach(context);
}
Button logic in Fragment A's onCreateView:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String newTextString = editTextView.getText().toString();
mOnDataListener.onDataReceived(newTextString);
}
});
MainActivity data receiver
public class MainActivity extends AppCompatActivity implements OnDataListener {
#Override
public void onDataReceived(String data) {
Log.e(TAG, "MainActivity received this string " + data );
}
}
Solution 1: Using Callbacks
//this will work if your fragment instance is active so check viewpager offscreenpage limit
(You can also use solution 3 mentioned by Farid Haq, which do the same by doing work of activity itself)
Here, Activity implements callback OnDataUpdateListener
interface OnDataUpdateListener{
void passDataToFragmentB(String data)
}
Activity code:
Fragment instanceFragmentB;
// intialise it somewhere in your activity
// or get from viewpager adapter
#Override
void passDataToFragmentB(String data){
instanceFragmentB.onDataChange(data)
}
Fragment A code:
OnDataUpdateListener mOnDataUpdateListener;
onAttach(Activity activity){
mOnDataUpdateListener= (OnDataUpdateListener) activity
}
onViewCreated(){
somebutton.onClick{
mOnDataUpdateListener.passDataToFragmentB("someString")
}
}
Fragment B code:
onDataChange(String data){
//do your work with update data passed from fragment A
}
Solution 2: Using EventBus or RxBus
//this will work if your fragment instance is active so check viewpager offscreenpage limit
Using event or rxbus, post new updated value on bus and make destination fragment observing that same value type.
Fragment A:
onViewCreated(){
somebutton.onClick{
EventBus.post("your data")
}
}
Fragment B:
#Subsribe
void onDataChange(String data){
//do your work with update data passed from fragment A
}
Solution 3: Using ViewModel.
Take viewmodel with context of activity in all fragment and Wrap String with LiveData.
Whenever, string data is changed, just post new data on String Livedata object.
All fragment observing that livedata will get updated value.
Solution 1
In your MainActivity
#Override
public void onDataReceived(String data) {
Log.e(TAG, "MainActivity received this string " + data );
Bundle bundle = new Bundle();
bundle.putString("edttext", data);
FragmentB fragment = new FragmentB();
fragment.setArguments(bundle);
// Fragment Transaction method goes here
}
In your FragmentB Retrieve Data
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false);
}
Solution 2
You can put callback data to your global variable and use it at fragment transaction at the time of onClickListerner.
Solution 3
A) Create method in your FragmentB
public void changeText (String data) {
textview.setText(data)
}
B) And Pass Value From MainActivity
#Override
public void onDataReceived (String data) {
myInterface.CallBack("Callbacked when onCreate method Created" + data);
Log.d("Tiggered", "respond: ");
FragmentManager manager = getFragmentManager();
FragmentB fragmentB = (FragmentB) manager.findFragmentById(R.id.id_fragmentB);
fragmentB.changeText(data);
}
There are a few posts about similar problems but I just don't get the wright answers and I hope that anyone can help me.
Situation:
I have a MainActivity that contains several Fragments. In one Fragment I start a CameraActivity with an intent. When the user has taken the picture, the CameraActivity gets closed with finish() and we return back to the previous Fragment.
Goal:
I give the Picture a certain name and would like to pass this name from the CameraActivity to the Fragment.
Problem:
Even though I call finish() in the CameraActivity and the screen returns back to the previous Fragment, the method onSaveInstanceState(Bundle outstate) is never called. Why is that?
Or how could I solve the problem otherwise?
Here is my code:
public class CameraActivity extends Activity {
// more code here
String imageFileName;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some methods here
finish(); // finish the current Activity and get back to previous Fragment
}
// more code here
#Override
protected void onSaveInstanceState(Bundle outState) {
Log.d(TAG_LOG, "onSaveInstanceState() called"); // is never called!
outState.putString("imageFileName", imageFileName);
super.onSaveInstanceState(outState);
}
}
And in the Fragment I do this:
public class MapFragment extends Fragment implements View.OnClickListener {
// more code here
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
// some methods here
if (savedInstanceState != null) {
String imageFileName = getArguments().getString("imageFileName");
Log.d(TAG_LOG, "image filename: " + imageFileName);
}
return view;
}
// more code here
}
Any help is highly appreciated!
You should take a look at startActivityForResults. Here is a short example (haven't tried it but you will get the idea) :
Fragment
Intent intent = new Intent(MainActivity.this,CameraActivity.class);
getActivity().startActivityForResult(intent,RESULT_PIC_TAKEN); // you may start an activity from another activity, not a fragment
CameraActivity
Intent results = new Intent();
results.putExtra("com;yourpackage.PIC_NAME", picName);
setResult(CameraActivity.RESULT_OK,results);
finish();
Back to MainActivity (since you called getActivity())
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_PIC_TAKEN) {
if(resultCode == Activity.RESULT_OK){
Bundle results = data.getExtras();
String picName = results.getString("com.yourpackage.PIC_NAME");
// code, and send what you want to the fragment
}
}
}
Hope this will help you!
Start the CameraActivity using startActivityForResult(), once you get the string to pass back in your previous screen, just create an Intent that will contain that string, and in your MainActivity, implement:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
Get the string from the intent, and pass it to your fragment.
onSaveInstanceState is used for other purposes.
I have this issue of sending some data back and forth between a fragment and its container activity, I succeeded in doing it. What puzzles me is sending my data from the fragment to the activity, at first I implemented OnResume(), OnStop() and sent the data through an intent and that created an infinite loop so I removed them. Then I did setRetainInstance(true) and it worked and gave me the wanted behavior.
My Question is How my data are really being sent and where in the fragment lifecycle ?
The Right approach is to use Interfaces. Don't use onStop or setRetainInstance()
See this. It will solve you problem.
Pass data from fragment to actvity
You can also achieve this by using Interface, using an EventBus like LocalBroadcastManager, or starting a new Activity with an Intent and some form of flag passed into its extras Bundle or something else.
Here is an example about using Interface:
1. Add function sendDataToActivity() into the interface (EventListener).
//EventListener.java
public interface EventListener {
public void sendDataToActivity(String data);
}
2. Implement this functions in your MainActivity.
// MainActivity.java
public class MainActivity extends AppCompatActivity implements EventListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void sendDataToActivity(String data) {
Log.i("MainActivity", "sendDataToActivity: " + data);
}
}
3. Create the listener in MyFragment and attach it to the Activity.
4. Finally, call function using listener.sendDataToActivity("Hello World!").
// MyFragment.java
public class MyFragment extends Fragment {
private EventListener listener;
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
if(activity instanceof EventListener) {
listener = (EventListener)activity;
} else {
// Throw an error!
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
// Send data
listener.sendDataToActivity("Hello World!");
return view;
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
}
Hope this will help~
How flow goes?
Activity 1 -----> Activity 2 (containing/inside) ------> Fragment
WhatI want to achieve?
Fragment (sends some data back to Activity 2) ----> Activity 2 (onBackPressed : collects that data & send it back to Activity 1) ---> Activity 1
How should I achieve above. I really don't want to use any variables/constants to cache the fragment data. Need to know any in-built method to handle this?
Moreover,
Activity 2 loads Fragment inside it.
In onBackPressed, I'm using setResult in Activity 2 to do standard data passing using startActivityForResult from Activity 1.
Also, if I write any method inside Fragment & call from Activity 2 using then due to that to/fro process a WHITE screen appears. So, really don't want to write own method & need to manage it while leaving the Fragment.
You should start Activity2 with startActivityForResult as below;
Intent i = new Intent(this, Activity2.class);
startActivityForResult(i, requestCode);
And in Activity2/fragment, you should finish acitivity as below;
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish()
And get result in Activity1 as below;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (this.requestCode == requestCode) {
if(resultCode == Activity.RESULT_OK){
//Get result
}
}
}
Hope it helps.
To pass data from Activity 2 to Activity 1 you can use startActivityForResult() in Activity 1 to start Activity 2, and onActivityResult() in Activity 1 to receive that data.
To pass data from Fragment to Activity 2, I'd suggest to use some interface like
interface OnSomeDataListener {
void onSomeData(SomeData data);
}
Then implement a setter method for this interface to a fragment like
public void setOnSomeDataListener(OnSomeDataListener listener) {
this.listener = listener;
}
And then in Activity, when instantiating a Fragment:
YourFragment fragment = new YourFragment();
fragment.setOnSomeDataListener(new OnSomeDataListener() {
void onSomeData(SomeData data) {
//return the result
Intent intent = new Intent();
intent.putExtra("data", data);
setResult(RESULT_OK, intent);
finish();
}
}
And finally, in the fragment, when you want to return some data to Activity:
if(listener != null) {
listener.onSomeData(dataToReturn);
}
From your FirstActivity call the SecondActivity using startActivityForResult() method
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your SecondActivity set the data which you want to return back to FirstActivity onBackPressed of SecondActivity.
#Override
public void onBackPressed() {
super.onBackPressed();
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
returnIntent.putExtra("bool",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
Now in your FirstActivity class write following code for the onActivityResult() method.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
boolean bool = data.getBooleanExtra("bool");
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//
To send data from Fragment to Second activity, you can use interface callback
public interface sendDataListener
{
void sendData(boolean foo);
}
Implement this listener to Second activity
Try to do this:
public class MyFragment extends Fragment{
private MyFragmentCommunicator myFragmentCommunicator;
....
myFragmentCommunicator.sendDataToActivity(name, image, isSend);
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
myFragmentCommunicator = (MyFragmentCommunicator)activity;
}
}
then declare your interface:
public interface MyFragmentCommunicator{
void sendDataToActivity(String name, String image, boolean isSend);
}
and then in your Activity do this:
public class MyActivity extends AppCompatActivity implements MyFragmentCommunicator{
#Override
public void sendDataToActivity(String name, String image, String price) {
//Manipulate the data
}
}
Hope it helps!!!
i acheived in Following way
In Activity write setters and getters
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
and in Fragment
filePath = ((YourActivity) getActivity()).getFilePath();
fileName=((YourActivity) getActivity()).getFileName();
if You are Using Same Fragment in More Than 1 Activity You can Create a constructor for that fragment and Pass context and check context is of which activity
public class BookmarkFragment extends Fragment {
private String filePath;
private String fileName;
Context contextCheckClass;
public BookmarkFragment(Context ctx ) {
this.contextCheckClass=ctx;
}
#SuppressLint("InflateParams")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
group= (View)inflater.inflate(R.layout.fragment_bookmark, null);
if(contextCheckClass instanceof FirstReaderScreen){
filePath = ((FirstReaderScreen) getActivity()).getFilePath();
fileName=((FirstReaderScreen) getActivity()).getFileName();
// ispurchased=((FirstReaderScreen) getActivity()).isIspurchased();
}
else if(contextCheckClass instanceof MainReaderScreen){
filePath = ((MainReaderScreen) getActivity()).getFilePath();
fileName=((MainReaderScreen) getActivity()).getFileName();
// ispurchased=((MainReaderScreen) getActivity()).isIspurchased();
}
return group;
}
for calling fragment
BookmarkFragment bookmarkFragment=new BookmarkFragment(FirstReaderScreen.this);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.LL_Fragment, bookmarkFragment)//LL_fragment is container
.addToBackStack(null)
.commit();