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

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.

Related

How to set activity result without finishing activity?

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

How to get value from another activity and bring back to previous fragment?

I have an Activity that have 3 fragment(FragmentA, FragmentB, FragmentC) like sliding tab. From FragmentB call another activity (lets call ActivityBB). After get item from Activity BB, How I can get value from ActivityBB and bring back to previous FragmentB ???
Well there are three ways that just comes in my mind. There may be more. But for now let me tell you those.
On ActivityBB put the values that you want to save in SharedPreferences. And then restart your activity. Well yes this might only work if you have values that can be arranged in key-value pairs. And is also not the proper way to do things. But will get your job done.
To restart an activity use this code. and then get your values from SharedPreferences.
Intent intent = getIntent();
finish();
startActivity(intent);
You can implement interfaces. This method is the best way to communicate between fragments. For more details check the google's documentation.
http://developer.android.com/training/basics/fragments/communicating.html
You can use Bundles. For that check this link.
How to pass a value from one Fragment to another in Android?
You can try some like this..
Pass your value into intent.
This code in your ActivityBB
Intent intent = new Intent(ActivityBB.this,ActivityBB.class);
intent.putExtra("yourDataKey",yourData)
startActivity(intent);
After that get your value into ActivityAA and load your Fragment with desired data
Intent intent = getIntent();
String yourValue = intent.getExtra("yourDataKey");
I solved this use intent and bundel with this flow :
MainActivity(FragmentA, FragmentB, FragmentC)
This activty(eq : from FragmentB) pass data using intent to ActivityBB
ActivityBB
at onClick ListItem in this activity, I pass data using bundle and call MainActivity (because I want to back to my previous fragment with item value from ActivityBB)
I make a condition from bundle at onCreate method at MainActivity to display currentItem(viewPager)
Actually its work, but I think this is not the proper way. I hope there is a solution with proper way from anyone to solve this.

How get the calling activity of an intent?

I have 2 list views :
- The first one is instantiated in a fragment
- The second one in a activity.
They display the same information
On click on a list item, I open another activity to display item detail.
Depending on the calling activity/fragment, I want to render a different layout, so I need to know in the activity that render the detail of a item, which activity created the intent ?
getCallingActivity and getParentActivityIntent seems to be declarative and fixe, isn"t it ? in my case there are both null.
How can I do that ?
You can pass an argument with your intent as:
intent.putExtra("ParentActivity", "ActivityA");
or
intent.putExtra("ParentActivity", "ActivityB");
In your next activity, use this:
String parentActivity = getIntent().getStringExtra("ParentActivity");
And render the layout according to result.
Hope it helps.

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.

Android Launching the current activity with different Intent Action

[Update Solution]
Referring to the post in the link
ViewPager PagerAdapter not updating the View
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
// And inside my PagerAdapter
#Override
public int getItemPosition(Object object) {
return 0;
}
Fixed all my problems, i just used Intent.setAction().
[Update to below Post]
My problem is i have a ViewPager PagerAdapter in my Main Activity. On clicking one of the 3 buttons., any specific intent will be fired (i used intent, i could have gone with just State variable as well, just that i pass some Uri with the intent). What happens is., i do
public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
Log.w(TAG,"Some button clicked !!");
getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);
mViewPager.getAdapter().notifyDataSetChanged();
}
This is why i was guessing maybe i should just do startActivity again, with the new Intent Action on the same Activity.
Spawning a new Activity, i would have to redraw every other view in the layout which is basically the same code, for the most part in the current activity.
Any suggestions here? Please help
[Original Post]
I have a PagerAdapter and 3 Buttons in the my Main Activity. This activity is enter from Main Launcher.
When i press any one of the buttons, the Intent Action is changed.
My question:
The changed Intent action reflects some changed view in the ViewPager and does_not spawn a new Activity as such, only the view is updated.
What approach should i take to get this task?
Can i start the currentActivity using startActivity() and different Intent actions on button click?
or is there any other efficient way in android to do this?
[No need code, just explanation of logic / method would suffice]
Thanks in advance
If you are saying that you are trying to use startActivity to bring up the same activity again, and its not working, it could be because you set something like singleTop in your Android manifest.
If you are asking whether or not you should use an intent to change the state of your Activity, then the answer is "it depends". If the user would expect the back button to return your app to its previous state (instead of going back to the home screen), then it might be a good choice for you. If that is the case, however, I would ask why not just make 2 different Activities? Otherwise, just do as Dan S suggested and update the state of your Activity as the user interacts with it.
You can always use the onNewIntent() hook. Do something like this in your activity:
protected void onNewIntent(Intent intent){
//change your activity based on the new intent
}
Make sure to set the activity to singleTop in your manifest. Now whenever startActivity is called the onNewIntent() will be executed. Also, note that per the documentation:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Categories

Resources