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.
Related
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.
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.
I have an main activity hosting three fragments. I want to pass data from another activity(which hosts a form) to one of the fragment in the main activity. Also the main activity uses viewpager to host the three fragments. How is that possible?
That depends on data you want to pass, but I guess that packing data into Bundle instance will do the job. When you create intent to launch another activity, you can set arguments on it. And later use that arguments when creating the fragments.
Take a look at this(from Intent class):
public Intent putExtra (String name, Bundle value)
public Bundle getExtras ()
To pass data between activites:
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
intent.putExtra("tag", dataToPass);
startActivity(intent);
If you want to pass a custom object like for instance an ArryList<CustomObject>, your custom object class have to implement Parcelable.
I want to send the Button View to another activity, eg.
Button bttn=(Button )findViewById(R.id.bttn)
startActivity(new Intent(this,Account.class))
by using bundles we can send values from 1 Activity to another activity,same way how i can send view to another activity..
You shouldn't send views between activities. Instead, create a separate button in the second activity and send only the necessary information.
It's simple..
Create a method like this in original activity:
ex:
public Button getButton(){
return bttn;
}
and declare buttonview as static.
By declaring it static you can access this view in another activity.
Call this method from another activity and get it and use it.
i have multiple activities in my activityGroup under tabactvity.
I want to start the child actvity for result.
Intent i = new Intent(this, addstocks.class);
View view = stocksgroup.group.getLocalActivityManager()
.startActivity("show_city", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
stocksgroup.group.replaceView(view);
this code basically taking me to the next activity. here i want to implement the actvity for result. hw can i do that?
It ts very late but just for reference a demo can be seen here: onActivityResult is not been invoked after the child activity calls finish()