How to set activity result without finishing activity? - android

I have a fragment(MyFragment) from where I am showing a popup chooser view as an Intent Activity (PopupActivity).
I need to pass chosen arguments to the fragment (MyFragment) without calling finish() on Activity (PopupActivity)
val intent = Intent()
intent.putExtra(intentKey, chosenValue)
setResult(ConstantIntentStatusCode.popupResult, intent)
This code works, when I call finish() on the PopupActivity. But I need to apply changes before activity will be closed.
Any suggestions? How to pass argument from child Activity to parent Fragment.
P.S. Could not take parent fragment from supportFragmentManager.fragments

Related

How to can I go from one activity to other activity view pager fragment

How to can I go from one activity to another activity view pager fragment and also that fragment contain view pager which contain two fragment and i want to go 2 nd fragment. I want to go 2nd fragment
I am trying replace fragment but it give No view found Exception .
and also try To Intent through i send value true value to the destination Activity and try to open that 2nd fragment . but I am not able to open that .
How to achieve that
Use an intent to specify the desired position of your viewpager before changing the activity
Intent mIntent = new Intent(A.this, B.class)
mIntent.putExtra("pager_position",1) // 0 in case you want the first fragment
startActivity(mIntent)
In the B Activity, the one with viewpager, make sure your viewpager and FragmentPagerAdapter are initialized before getting the data from the intent.
After that you simply put this:
Bundle extras = getIntent().getExtras();
int pageIndex = extras.getInt("pager_position")
mViewPager.setCurrentItem(pageIndex)
In second Activity set mViewPager.setCurrentItem(1)

How not to remove child activity and go back to parent activity?

What I am trying to do is kept all the data in a child activity and go back to a parent activity .
In other words, as far as stack structure, the child activity, which is located at the top of stack structure, should be kept and go back to parent activity.
Thank you for your help in advance
One method you could use is starting the child activity using startActivityForResult(). Then once the child activity is closed, data is passed back to the parent activity.
If you then want to start the child activity again with the same data as before, you can check to see if the parent activity has any data from a previous result and parse it to the new child activity in the intent.
ParentActivity
String mData = null; //this goes above the parent's onCreate method, using a string as an example
//this is where you start the child activity
Intent intent = new Intent(context, ChildActivity.class);
if(mData != null) {
intent.putExtra("MyData", mData);
}
startActivityForResult(intent, mRequestCode);
ChildActivity
onCreate...
if(getIntent().getStringExtra("MyData") != null) {
//handle restoring the child activity to its previous state
}
In you parent activity you could even get rid of the if statement as the child activity checks if the data sent is null.
There can be only one active activity at one moment of time. If you need to save state, serialize it to SharedPreferences or to your Database
I suggest to use Fragment instead of activity. It is a better way I think than using activity.

Hi i want to redirect from my actvity to another fragment

This my code to redirct from Viewactvity to Fragment_home (Fragment_Home,which is already used in my Mainactivity)
Intent intent = new Intent(Viewactivity.this,
Fragment_Home.class);
String responseMsg = jObj.getString("response");
Toast.makeText(getApplicationContext(),
responseMsg, Toast.LENGTH_LONG).show();
System.out.println("Your" + responseMsg);
startActivity(intent);
My Fragment is Fragment_Home ,i didnt use Fragment manager in the Viewactivity class,So can you please help me to navigate from my current activity to Fragment_Home.
Fragment is not any activity that is contained in Activity, therefore you can add, replace, remove fragment there, not startActivity.
you can start activity from fragment not fragment from activity.
fragments are simply pushed in stack within an activity if required.
you can also attach the fragment to your other activity in its live state(i.e. fragment is not removed so that its data can be used by your another activity)
check this for details:
http://developer.android.com/reference/android/app/Fragment.html

Fragments, Parameters, and ActionBar 'Up' Navigation

So I am trying to get some experience with Fragments, but I'm finding some roadblocks.
My current situation is as follows.
I have an activity that displays a List whose content is determined by Extra Intent parameters sent from the 'calling' activity.
This List activity uses ListFragment declared in the XML like so:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#color/black">
<fragment class="com.pixlworks.NLC.DirectoryBrowse$ListingFragment"
android:id="#+id/listing"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Currently I get the parameter that indicates the type of content directly in the Fragment by accessing the Extra data of the Activity Intent (or saved Bundle if available):
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null)
mListingType = savedInstanceState.getString(Utils.DIRECTORY_TYPE_STORE_KEY);
else
mListingType = getActivity().getIntent().getStringExtra(Utils.DIRECTORY_TYPE_STORE_KEY);
// get content by type, create and set the adapter
}
Now part of my problem is that I am not sure this is the right way to 'pass' that parameter from the Activity to the Fragment.
On top of that, I am getting issues with this setup when using the Action Bar's UP Navigation. When I click on an item in this List Activity it goes to another activity showing the details of the selected item. From this detail activity:
If I use the back button, the List Activity is brought back from the stack as usual and everything works fine.
If I use the ActionBar's UP (despite following steps here), it would seem that a new instance is created instead of using the one in the stack and this new instance obviously is not getting the Extra parameter in the Intent. Since I am expecting the value to exist in the saved Bundle or in the Intent, my app crashes in this situation.
So to boil things down, I am not sure which of these to follow and how to make them work properly with 'UP' navigation:
A) Hold the 'type' parameter in a field in the Activity and save it in the Activity's Bundle onSaveInstanceState. In which case I am not sure how to then pass the value to the Fragment. In this case I would just need to make sure that UP calls the existing instance of the Activity List
B) Continue with my current setup of saving the value in the Fragment instead of the Activity, but again, how to handle the UP navigation correctly?
I know it is kind of multiple things I am asking here at the same time, but they are all connected, so I hope that I can get some help on this.
Thanks for any help in advance!
The UP navigation makes more sense to be used within the same activity level. That is the intention of the codes that you followed in the developers page. Because you started a new activity, if you want to return to previous activity like the back button you will need to call finish() to destroy the details activity first.
As for passing data from activity to fragment, when you create a new instance of fragment, you can pass the data to it as bundle, for example:
// in fragment class
public static MyFragment newInstance(Bundle arg) {
MyFragment f = new MyFragment();
f.setArguments(arg);
return f;
}
When you create a new fragment, you can call:
// in activity
Bundle arg = new Bundle();
int info = ...;
arg.putInt("INFO",info);
...
MyFragment mFragment = MyFragment.newInstance(arg);
Finally, to get the data in fragment:
int info = getArguments().getInt("INFO");
...
Instead of directly calling MyFragment mFragment = new MyFragment() to instantiate the fragment, you should use a static method to instantiate it. This is to prevent some crashes which might happen if you rotate the screen and the framework complains that it couldn't find a public empty constructor.
UPDATE
To answer your questions:
1) Say you start from activity A -> activity B. Then in activity B you press the up button. By logic of use, the up button will not bring you back to activity A, because its intention is to navigate one level up,but still inside, activity B. To return to activity A, you need to call finish() to destroy activity B first.
2) If your fragment is created in xml, you still can set arguments. In your xml, you set an id for the fragment android:id="#+id/fragment_id", then
// in activity
FragmentManager fm = getSupportFragmentManager(); // or getFragmentManager() if you don't have backward compatibility
MyFragment mFragment = fm.findFragmentById(R.id.fragment_id);
Bundle arg = new Bundle();
// put data blah blah
mFragment.setArguments(arg);
Just make sure you set the arguments before you use the fragment.
Simply said, intent is used when you pass data between calling activities; bundle is used when you want to pass data from activity to fragment.

Passing extra Intent information from Activity to View

I have an activity that is launched from another activity through an intent. The intent carries an extra "id" information. Now, the launched activity has a custom view (actually, a extension of LinearLayout class). I want to access the "id" information in the custom view. Can the activity pass that value to its contained view? Or can the view get a handle to the activity?
YES,
First you have pass id with the intent like
Intent i=new Intent(getApplicationContext(), sample.class);
i.putExtra("id", id);
startActivity();
it pass the value to sample class
here
String i=getIntent().getExtras().getSerializable("id").toString();
& you can use this id in your custom view
Yes, your custom View class can get a reference to the Activity it is contained in. Every View has a getContext() method which returns the Context the View is running in (i.e. your Activity).
make a method in your view that takes the id as a parameter, and then call that method from your activity.

Categories

Resources