Passing data between 2 activities - java

I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!

You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");

You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");

your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");

Related

how to open fragment from another activity android

i am trying to use startactivityfromfragment()
i didnt find sumthing usefull for me to be a good example
until now i was able to move data(string of web url) from fragment to fragment
now its a diffrent story.. i think i need to use startactivityfromfragment()..
but dont know how
Bundle bundle = new Bundle();
bundle.putString("WEB","someweb.com");
MultiFragment multiFragment = new MultiFragment();
multiFragment.setArguments(bundle);
Intent intent1 = new Intent(getBaseContext(), MultiFragment.class);
startActivityFromFragment(MainActivity.class,intent1,"WEB");
Try this:
Put this code in current fragment, from where you want to open an activity.
Intent i=new Intent(getActivity(),Multifragment.class);
i.putExtra("key","value");
getActivity().startActivity(i);
In the next activity which contains fragment, put these:
String value=getIntent().getStringExtra("key");
Bundle bundle=new Bundle();
bundle.putString("key",value);
YourFragmentClass ob=new YourFragmentClass();
ob.setArguments(bundle);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.your_container_layout,ob).addToBackStack(null).commit();
Then, In YouFragmentClass:
Bundle=getArguments();
if(bundle!=null}
String value=bundle.getString("key");
Use your Activity name as second parameter in which the your MultiFragment exist
Intent i = new Intent(MainActivity.this, ActivityName.class);
putExtras(bundle);
startActivity(i);
finish();
Method 1
For sending data from activity to another activity's fragment call a normal intent with bundle value, In another activity check on getIntent() and call a method from that fragment
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("web_url", "url");
startActivity(intent);
and call from another activity
String webUrl = getIntent().getStringExtra("web_url");
Fragment newFragment = getSupportFragmentManager().findFragmentById(R.id.frame_tab);
newFragment.openWeb(webUrl);
Method 2
In second activity make a static variable and save the data from getIntent() and in fragment use that variable.
Mehtod 3
interface FragmentListner{
void openFragment(String url);
}
Activity A :
FragmentListner frag = new FragmentListner();
frag.openFragment("Url");
Activity B:
class B implements FragmentListner{
void openFragment(String url){
//Open Fragment here
}
I think:
In an activity that doesn't contain fragment:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("something", "yep");
startActivity(intent);
And, in the next activity which contains fragment:
Intent intent = getIntent();
String op = intent.getStringExtra("something");
if (op != null){
getSupportFragmentManager().beginTransaction().replace(R.id.nav_host_fragment,
new YourFragment()).addToBackStack(null).commit();
}

Android Studio: Passing variable through another variable doesn't work

I created a Method for my MainActivity to pass a String to my SecondActivity.
public void convertCurrency(View view)
{
intent.putExtra("NO", "My String");
startActivity(intent);
}
But in my SecondActivity in my OnCreate Method
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
Intent intent = new Intent(this, MainActivity.class);
String g = intent.getStringExtra("NO");
t.setText(g);
Nothing happens. But Why?
get Your variable in this way:
String yourVriable = getIntent().getStringExtra("NO")
don't new Your intent
Your are creating new intent.
In onCreate method call
String data = getIntent().getStringExtra(key);
replace Intent intent = new Intent(this, MainActivity.class);
by Intent intent = getIntent();
When you do intent.getStringExtra("NO"); your intent Object is new, so his Extra is empty.
You need to change the code in both the activities . You need to create intent and put your string into it, so your code will be.
public void convertCurrency(View view) {
Intent intent = new Intent(this, Main2Activity.class);
intent.putExtra("NO","My String");
startActivity(intent);
}
Note that in the intent constructor the first argument is the current activity and the second argument will be the activity that you are starting.So in the second activity you need to recieve the string. Code for that will be,
Button back = (Button) findViewById(R.id.back);
TextView t = (TextView) findViewById(R.id.first_text);
String g = getIntent().getStringExtra("NO")
t.setText(g);
I resolve the problem myself. There occurs an error if you have two Intent objects with the same name even if they are within separate methods.

How do I get the reference of an activity I opened using intent?

Inside my MainActivity I open my SecondActivity
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
How do I get the reference of this instance of SecondActivity inside my MainActivity?
//inside MainActivity
SecondActivity object = // make it equal to instance created above
Pass your a handler to the second one via trigger intent, like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Handler", handler);
startActivity(intent);
Then in your SecondActivity post message and data, maybe the instance of SecondActivity.this or another you like, to the handler.

What is the difference between getIntent() and new Intent() in Android?

I do like this
first way
Intent intent = new Intent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
second way
Intent intent = getIntent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
Both can give the same result.I want to know the actual difference between this two
In the context of an Activity, getIntent() will return the Intent that was originally sent to the Activity. The example you gave may work the same, but you should really avoid using getIntent() if you are passing the Intent to another Activity or sending it back as a result.
For example:
If I start an activity with:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("key", "test");
startActivity(intent);
Then in my MainActivity class:
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
So now consider if you have SecondActivity and I am starting it from MainActivity using getInent();
Intent intent = getIntent();
intent.setClassName("com.example.pkg", "com.example.pkg.SecondActivity"");
intent.setComponent(new ComponentName("com.example.pkg", "com.example.pkg.SecondActivity"));
intent.putExtra("isLoggedIn",true);
startActivity(intent);
Then in my SecondActivity I can access key and isLoggedIn both.
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
boolean testIsLoggedIn = intent.getBooleanExtra("isLoggedIn",true);
So, generally it is not good practice to use the getIntent to start further activities.

Action before startActivity()

I'm new in Android development and I've a problem when I create my new activity.
I want to use my activity before start it. For example, I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,MyActivity.class);
Screen.this.startActivity(intent);
But, my "menu" and "MyActivity.class" aren't the same instance. Consequently I have try it:
MyActivity menu = new MyActivity();
menu.setXmppreception(reception);
Intent intent = new Intent(Screen.this,menu);
Screen.this.startActivity(intent);
But it doesn't work...
Have you a solution for help me?
Thanks for help and sorry for the bad english.
You can't do that like you want, if you are looking to pass data between an activity you must use Extras, and you can pass just Serializable items.
First Context (can be Activity/Service etc)
You have a few options:
1) Use the Bundle from the Intent:
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value);
2) Create a new Bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
3) Use the putExtra() shortcut method of the Intent
Intent mIntent = new Intent(this, Example.class);
mIntent.putExtra(key, value);
New Context (can be Activity/Service etc)
Intent myIntent = getIntent(); // this getter is just for example purpose, can differ
if (myIntent !=null && myIntent.getExtras()!=null)
String value = myIntent.getExtras().getString(key);
}
NOTE: Bundles have "get" and "put" methods for all the primitive types, Parcelables, and Serializables. I just used Strings for demonstrational purposes.
you don't have to create the new activity yourself, Android system does it for you.
If you want to go from Screen Activity to MyActivity, you can do this :
Intent intent = new Intent(Screen.this,MyActivity.class);
startActivity(intent);
and then, in you MyClass java file, in the onCreate method, you could do :
this.setXmppreception(reception);
This way, I think you get you wanted, no?

Categories