Android get different content in single activity - java

actually I'm searching a way to show different content in one activity everytime when it's created.Here is what actually I'm thinking to do but not really sure if there is a way and how can I do it. Basically I have two activities. The first one contains a listview with 100 elements on it.I want to be able to show different content in activity 2 when I click a listview item in Activity 1. I need to be able to change two textviews and one imageview.
Any suggestions how can I do that? Thanks in advance!

You want to use Intents to pass Payload between your Activitys.
On Activty1 you make a new Intent like:
Intent myIntent = new Intent(view.getContext(),
Activty2.class);
myIntent.putExtra("detailtext", ((TextView) view).getText());
startActivityForResult(myIntent, 0);
the putExtra Method is for your Payload.
then in Activty2 you can extract the Intent with:
getIntent().getStringExtra("detailtext"));
hope that helps

Do somethins like this :
Activity 1:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("someKey","someValue");
startActivityForResult(Intent, 0);
Activity 2:
String i = getIntent().getStringExtra("someKey"));
TextView txt = (TextView) findViewById(R.id.textView); //your textview's id
txt.setText(i);
That should work!

Related

How to return back to a fragment in viewpager from an Activity's fragment

I have a viewpager with 2 fragments(A and B) in MainActivity. In the fragments in viewpager, I have a recyclerView for each fragment that contains a list of items. When I click on an item of a fragment viewpager, it displays a new Activity with a new fragment (C) on it. But when I press the back button on toolbar, it always went back to the MainActivity with fragment A of viewpager, even if I click on item of fragment B.
In androidManifest.xml, I added "parentActivity: MainActivity" to the new Activity. If I don't set that, the back button would not response.
I need help to go back to the correct fragment when I press back button.
I appreciate all answers.
Very easy. Keep an index variable in your activity in which viewpager and fragments are loaded. When you switch to another activity, do it via this activities method and update that variable with the index of the current selected page/fragment in viewpager by
getCurrentItem
This method will return the index which you can store then there are two options for you: If you know that your activity is going to be resumed from that activity only then you can write below method in onResume:
setCurrentItem
Or else you can go for startActivityForResult and achieve the same. Let me know if any queries.
First of all let assume you pressed something to open C fragment from B fragment. Here you need to pass some value to inform that C was opened from B fragment.
Send data from fragment to activity
Intent intent = new Intent(getActivity().getBaseContext(),
TargetActivity.class);
intent.putExtra("B", message);
getActivity().startActivity(intent);
Here value "B" means that activity with fragment C is opened by fragment B. For A fragment you need to change it to "A" or whatever you want.
Receive data in activity and send it back if Back button pressed
Intent intent = getIntent();
String message = intent.getStringExtra("message");
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.class, FirstActivity.class)
intent.putString(message)
}
});
Receive data in FirstActivity and set necessary page
Intent intent = getIntent();
String message = intent.getStringExtra("message");
if(message == "B"){
viewPager.setCurrentItem(1) //1 is index of your second page
}else {
viewPager.setCurrentItem(0)
}
Try this code and let me know if something goes wrong
First, for going to "NewActivity" just start the activity using intent and not finish the "MainActivity".
And For coming back to "MainActivity", just finish the "NewActivity"... hopefully, this will work!

how to get from a fragment to an activity without disturbing fragment's reausability

This is now bugging me a lot.
I want to get to an activity from a Fragment without disturbing the reusability of the fragment. That mean I cant directly start an Intent from within the fragment to a the activity the fragment is currently attached to.
Here is the code under question
//This is the code inside the fragment
public void onStart() {
super.onStart();
View view = getView();
switch (position){
//case 1 is the case when the user clicks the ADD List Item from a ListFragment.
//Position is the position of ADD in the List Fragment.
case 1:{
Intent intent = new Intent(/*what exactly should I put here?*/ ,
/*this is where the reference to activity the fragment is attached to goes.
But we dont know what activty the fragment is attached to, as it is reusable
and may get attached to different activity at different times*/);
}
case 2:{
//this is the case when user decides to view the entered text in the array list.
TextView textView = (TextView)view.findViewById(R.id.display_name);
int size = workout.arrayList.size();
Object[] array = workout.arrayList.toArray();
for(int i=0;i<array.length;i++){
textView.setText((String)array[i] + "\n");
}
}
}
}
I feels like the data may be insufficient, although I am not sure what else to provide, sorry about that.
If more data is required, tell me.
Inside a Fragment we get the Context for an Intent using:
Intent intent = new Intent(getActivity(),AnyActivity.class);
OR
Intent intent = new Intent(getView.getContext(),AnyActivity.class);
The AnyActivity() can be any activity including the one currently having the Fragment.
You can open the same activity like this..
Intent intent = new Intent(getActivity(),SameActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
UPDATE To reuse the fragment for different values pass arguments to the fragment...during transaction..
Bundle bundle = new Bundle();
bundle.put("className", "SameActivity");
fragment.setArguments(bundle);
in fragment get the class name from bundle ... different class will pass different arguments as bundle..
In OtherActivity bundle would look like this..
Bundle bundle = new Bundle();
bundle.put("className", "OtherActivity");
fragment.setArguments(bundle);

How to pass String from Fragment to Activity

I'm passing an url as string from Fragment to Activity. But I'm getting an error of could resolve getArguments() and it is highlighted in red color
Inside Fragment
Intent i = new Intent(getActivity(), web.class);
Bundle args1 = new Bundle();
args1.putString("url1", "file:///android_asset/em/japan.html");
startActivity(i);
((Activity) getActivity()).overridePendingTransition(0,0);
and inside activity where I want to receive string, I've used
String url1 = getArguments().getString("url1");`
But getArguments() is highlighted in red color.
Thanks in advance.
Intent i = new Intent(getActivity(), web.class);
i.putExtra("url1", "file:///android_asset/em/japan.html");
startActivity(i);
and in your activity use
String url1 = getIntent().getStringExtra("url1");
You need a callback for that, see this: https://developer.android.com/training/basics/fragments/communicating.html
comment below if you need do a sample project for your case :D

send image from one activity to another

I am trying to send an image from one activity to another but I dont know how to set the imageview.
here is how I send the image and other stuff
Intent item_intent = new Intent(MainActivity.this, Item.class);
item_intent.putExtra("name","test name arif");
item_intent.putExtra("quantity","99");
//*************************here is the image***************************
item_intent.putExtra("image",R.drawable.access);
MainActivity.this.startActivity(item_intent);
here is how I am trying to read the image and set it to the ImageView but I am getting a syntax error.
Intent intent = getIntent();
ImageView img_view = (ImageView) findViewById(R.id.item_image);
// this where I am having problem below******************************
img_view.setImageBitmap(intent.getByteArrayExtra("image"));
how should I set the ImageView?
Dont you think
img_view.setImageBitmap(intent.getByteArrayExtra("quantity"));
should be
img_view.setImageResource(intent.getIntExtra("name",R.drawable.default_image));
use this code for get intent
Intent intent = getIntent();
ImageView img_view = (ImageView) findViewById(R.id.item_image);
img_view.setBackgroundResource(intent.getIntExtra("name",1));

Why overridePendingTransition does not take effect when I jump from Fragment to a new Activity?

The first activity contains a Fragment, and there's a button inside the fragment, and when I click this button, I wish to jump to a new Activity with different animation. So I do it like this in the Fragment:
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), NewActivity.class);
getActivity().startActivity(intent);
getActivity().overridePendingTransition(
R.anim.forward_activity_move_in,
R.anim.forward_activity_move_out);
});
However the new animation does not take effect when moves to the new Activity, so I wonder maybe I do it wrong. I hope someone can help me, thx a lot:)
Thanks, I solved this problem, You can find solutions at Change activity transition when inside a TabHost
Well the problem is not the case of Fragment, instead the TabActivity causes the problem.

Categories