Passing String from Activity back to running Fragment in Android - android

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.

Related

Restore fragment state after ActivityResult

I'm working on a fragment which displays a list of images. It has a button for "add image" that starts an intent for result with these values.
type: "image/*"
action: Intent.ACTION_GET_CONTENT
The problem is: after the user picks an image and returns to the fragment, all the other images in the list (stored in some ArrayList<> on the code) are gone.
I've overridden the method onSaveInstanceState(Bundle) and saved the list inside the bundle. The thing is, there's no way to restore it back.
I thought of overriding onViewStateRestored(Bundle) but it didn't work. When I put some Log.d() on all "onXXX" methods, I found that only these three are executed every time I add a file (actual order):
onPause()
onSaveInstanceState(Bundle)
//now the image picker opens up
//user picks the image
onResume()
//image picker closes and fragment is now on screen
I thought of using some "getXXX" method at onResume() but I can't find one that's useful. What can I do?
EDIT: Here is my code (without irrelevant stuff).
#Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.d("debugaff", "oncreate");
setRetainInstance(true);
this.attachments = (ArrayList<Attachment>) getActivity().getIntent().getSerializableExtra(ExtraKeys.ATTACHMENTS);
}
#Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
Log.d("debugaff", "oncreateview");
rootView = inflater.inflate(R.layout.fragment_attachments_form, container, false);
//....
if(savedInstanceState == null){
getLoaderManager().initLoader(0, null, this);
} else {
attachmentsRvAdapter.setItems((List<Attachment>) savedInstanceState.getSerializable(ExtraKeys.TEMP_ATTACHMENTS));
}
//....
return rootView;
}
#Override public void onResume(){
super.onResume();
Log.d("debugaff", "onresume");
hideKeyboard();
}
#Override public void onPause(){
super.onPause();
setRetainInstance(true); //called this to make it 100% redundant (already called it at onCreate)
Log.d("debugaff", "onpause");
}
#Override public void onViewStateRestored(#Nullable Bundle savedInstanceState){
Log.d("debugaff", "onviewstaterestored");
if(savedInstanceState == null){
getLoaderManager().initLoader(0, null, this);
} else {
attachments.addAll((List<Attachment>) savedInstanceState.getSerializable(ExtraKeys.TEMP_ATTACHMENTS));
attachmentsRvAdapter.setItems(attachments);
}
super.onViewStateRestored(savedInstanceState);
}
I have just mimic your situation and found no error (list saves the new value with old values in the list). The only problem I can think of is you are not initializing your list correctly. What I did was to initialized the list in the onCreate() method and when I pick another Image it saves that image in the same list with the previous image still in the list. There is no need to save the list separately in the savedInstanceBundle. Here is my Fragment code:
public class SelectListFragment extends Fragment {
private static final int PICK_IMAGE_REQUEST = 11;
public SelectListFragment() {
// Required empty public constructor
}
List listOfImagesSelected;
Button selectButton;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listOfImagesSelected=new ArrayList<Bitmap>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_select_list, container, false);
selectButton = view.findViewById(R.id.selectButton);
selectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//I am retrieving the Bitmap from the intent and saving into the list
if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
//making the bitmap from the link of the file selected
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
listOfImagesSelected.add(bitmap);
}catch (Exception e){
}
}
}
}
I can share the Complete Working Code of my Project if needed. For better understanding of the Fragment LifeCycle please review this Image
To use onViewStateRestored, you need to call 'setRetainInstance(true);'

How to get data from fragment to parent activity & pass it to back activity?

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();

Lifecycle when calling the fragment replace or open new activity?

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.

How to call startactivityforresult from a non-activity class to get the resuts

Is it possible to call startActivityForResult() from a non-activity class to get the results?
Scenario is something like this:
I have a class NonActivity (it doesn't derive from Activity as its not a UI).
This class will have bunch of functions(steps basically) to run.
One of the steps requires to show UI(Activity) and then get the result (user enter something).
Then been able to use that data in next following steps.
How can this be achieved without deriving from activity class as I don't have UI component?
Also since I don't want to derive from activity class that means I cannot override OnActivityResult(). Where results actually come from?
startActivityForResult() is only available from real on-screen activities, since it is a method in, well, Activity. Please redesign your application so that the user interface is driven from activities.
On the other hand, if your non Activity class is initialized and used from an onscreen Activity, you could pass that instance of the Activity to your class as a parameter in the constructor and use it to launch other Activities.
Be careful though. Using this method increases the risk of a memory leak, as the external class (Utils in my example) might keep a reference to the Activity even after its gone.
If all you want to do is access data, then you could try writing it to SharedPreferences or a Database or some files and then using the application context (passed in via a constructor again) to read it. This reduces the risk of a memory leak. Something like:
MyApiClass myApiClass = new MyApiClass(getApplicationContext());
EXAMPLE CODE
Main Activity:
public class Main extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils util = new Utils(this);
util.startTest();
}
#Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
Toast.makeText(this, "onActivityResult called", Toast.LENGTH_LONG).show();
super.onActivityResult(arg0, arg1, arg2);
}
}
Utils class (which launches for result):
public class Utils {
Activity activity;
public Utils(Activity ac) {
activity = ac;
}
public void startTest() {
Intent i = new Intent(activity, Test.class);
activity.startActivityForResult(i, 1);
}
}
Test Activity:
public class Test extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();
this.setResult(Activity.RESULT_OK);
this.finish();
}
}
StartActivityForResult from a class using a fragment with no visible GUI. You might find something like this in a utility class.
see runGetUserAccount below. It creates its own fragment and executes a startActivityForResult. Then it has it's own onActivityResult.
public class MyGooglePlay {
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 31502;
private ActionBarActivity activity;
private FragmentManager fragManager;
public MyGooglePlay(ActionBarActivity activity) {
this.activity = activity;
this.fragManager = activity.getSupportFragmentManager();
}
/**
* Starts an activity in Google Play Services so the user can pick an
* account
*/
private String mEmail = "";
static final int REQUEST_CODE_PICK_ACCOUNT = 1000;
public void runGetUserAccount() {
if (TextUtils.isEmpty(mEmail)) {
// run this code in gui less fragment so we can pickup the
// on activity result from inside the mygoogleplay class.
Fragment f = new Fragment() {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
String[] accountTypes = new String[] { "com.google" };
Intent intent = AccountPicker.newChooseAccountIntent(null,
null, accountTypes, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_PICK_ACCOUNT);
}
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == REQUEST_CODE_PICK_ACCOUNT) {
if (resultCode == Activity.RESULT_OK) {
set_Email(data
.getStringExtra(AccountManager.KEY_ACCOUNT_NAME));
// getUsername();
}
super.onActivityResult(requestCode, resultCode, data);
}
//this is to verify the fragment has been removed.
//you can log or put a breakpoint to verify
#Override public void onDestroy(){
super.onDestroy();
}
};
FragmentTransaction fragmentTransaction = this.fragManager
.beginTransaction();
fragmentTransaction.add(f, "getusername");
fragmentTransaction.commit();
}
}
/**
* #param mEmail
* the mEmail to set
*/
private void set_Email(String mEmail) {
this.mEmail = mEmail;
if (!TextUtils.isEmpty(mEmail)) {
// TODO notify caller email is ready;
// activity.onEmailReady(mEmail);
}
//we are done with the "getusername" fragment
Fragment f = fragManager.findFragmentByTag("getusername");
if (f!=null) {
fragManager.beginTransaction().remove(f).commit();
}
}
}
U should pass context as Activity,then u will get solution.
try this below code.it will work
In non Activity class
public class nonActivity {
public static void method(Activity activity)
{
Intent intent = new Intent(activity, SecondActivity.class);
activity. startActivityForResult(intent, REQUEST_CODE);
}
}
In SecondActivity
Intent intent = getIntent();
intent.putExtra("data", "data"); //here u can pass data to previous activity
setResult(RESULT_OK, intent);
finish();
In firstActivity
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String status = data.getStringExtra("data");
//Do what u want with data
}
} catch (Exception e) {
System.out.println("=====Exception=====" + e.toString());
}
}
If you want the result back from the activity to your normal class, supposed it is a class with a custom adapter within it.
you cannot use startActivityForResult because you are not in an activity
what I did is that i launched the activity from the class with an intent. Then I calculated or did what I have to. From this activity I send the information to the main class supposed with a method MainActivity.the_method() and in the main activity I changed the custom adapter o did what I have to using the adapter object and calling adapter.getItem(position)
Hope this can give you an idea

how to use startActivityForResult() in ActivityGroup?

I use a TabActivity with some tabs,each tab contain a ActvityGroup,each ActivityGroup manage more than one Activity.one of ActivtyGroups have three Activies:A,B and C.
At first A is created,when user click a button in A,it jump to B.
in B there are some important data which can be edited in C,when click a "edit"button in B,it jump to C.
if some data is edited in C,when i click back button,i want modify the same data in B.
what drive me crazy is when i use "finish()" in class C,my app exit directly.
I had searched many solutions on the net,bu none of them fit for my case,I don't know where is wrong,please help me or give me a example of how to use startActivityForResult() in ActivityGroup
here is my group:
public class MyGroup extends ActivityGroup
{
private int ID=0;
private AlertDialog dialog;
private Stack<View>history;
private LocalActivityManager manager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
history = new Stack<View>();
manager = getLocalActivityManager();
Intent intent = new Intent(this,A.class);
startActivity(intent);
}
#Override
public void startActivity(Intent intent)
{
View view = manager.startActivity(""+ID++,intent).getDecorView();
history.push(view);
setContentView(view);
}
#Override
public void startActivityForResult(Intent intent,int requestCode)
{
// super.startActivityForResult(intent, requestCode);
View view = manager.startActivity(""+ID++,intent).getDecorView();
history.push(view);
setContentView(view);
}
/*
* if user edited data in C.class.
* when C.class finished,refresh data in the B.class
*/
#Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
Log.e("MyGroup","running");
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
//modify data in B.java
B subActivity=(B)(manager.getCurrentActivity());
subActivity.handleActivityResult(requestCode, resultCode, data);
}
}
/*
* when press back button, manage which page to show next
* if there is one page in stack,that means when press back button it will
* exit the app,so we add a dialog to notify user whether exit app or not
*/
#Override
public void onBackPressed()
{
int size=history.size();
if ( history.size()>= 2)
{
history.remove(size-1);
setContentView(history.get(size-2));
}
else
{
if(dialog==null)
{
createDialog();
}
dialog.show();
}
}
}
in B.class:
public void nextPage()
{
Intent intent=new Intent(B.this,C.class);
intent.putExtra("name", productAdapter.getName(position));
intent.putExtra("id", productAdapter.getID(position));
B.this.getParent().startActivityForResult(intent,11);
}
/*
* modify data in modifyItem
*/
public void handleActivityResult(int requestCode,int resultCode,Intent data)
{
String price=data.getExtras().getString("price");
String name=data.getExtras().getString("name");
String quantity=data.getExtras().getString("quantity");
productAdapter.setName(name, modifyItem);
productAdapter.setPrice(price, modifyItem);
productAdapter.setQuantity(quantity, modifyItem);
productAdapter.notifyDataSetChanged();
}
in C.class:
#Override
public void onBackPressed()
{
if(price!=null)
{
Bundle bundle=new Bundle();
bundle.putString("name",name);
bundle.putString("price",price);
bundle.putString("quantity",quantity);
this.getParent().setResult(RESULT_OK,new Intent().putExtras(bundle));
this.finish();
Log.e("C","inner");
}
Log.e("C","outer");
this.getParent().onBackPressed();
}
Why do you call finish? Pressing back means it will destroy
Your Activity from a ActivityGroup will not get the response calls directly.
You need to redirect from ActivityGroup.
Please see the below answer
Here is the solution. Please try this
https://stackoverflow.com/a/15047518/1403112

Categories

Resources