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

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.

Related

Is there a way to check if I came from a specific activity before starting a new one?

Let's say I have 3 activities: A, B, and C. And all 3 activities can direct the user to one another. Is there a way to check if I came from a specific activity?
For example, to get to C, I can move from B -> C or A -> C. But is there a way for me to check if I came from B?
You can pass data payload through activity intent.pass your activity name for check where user came from.
start activity
Intent intent = new Intent(yourFromActivity.this,yourToActivity.class);
intent.putExtra(bundle_key,yourActivityName);
startActivity(intent)
In your second activity check intent extra and get the activity name where user came from.
#override
protected void onCreate(Bundle savedInstanceState){
//check extra key has in bundle.should same as host activity key
if(getIntent().hasExtra(bundle_key))
{
//get passed value from intent bundle
String activity = getIntent().getStringExtra(bundle_key,"");
if(activity.equel(yourActivityname)){
//put logic here
}
}
}
You can do it by 2 ways which are
Pass extra parameter in Intent when calling activity and in receiver
activity check received parameter and perform task accordingly.
Use startActivityForResult() instead of startActivity() when calling
activity & use getCallingActivity().getClassName() in receiver call to get
class name.
Passing Data in Intent:
Calling class A:
Intent intent = new Intent(A.this,C.class);
intent.putExtra("source","A");
startActivity(intent);
Receiver class C:
in onCreate method
String source;
Intent intent = getIntent();
if(intent.hasExtra("source"))
{
source = intent.getStringExtra("source");
}
//Now you received source class name you can check and perform action
//accordingly.
if(source.equals("A")
{
//For Class A
}
else{
// For Class B
}
Using startActivityForResult():
Sender Class:
Intent intent = new Intent(A.this,C.class);
startActivityForResult(intent);
Receiver Class:
//In onCreate Method get calling activity name
getCallingActivity().getClassName();
try this
Intent intent = new Intent();
intent.setClass(A.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_A");
A.this.startActivity(intent);
Intent intent = new Intent();
intent.setClass(B.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_B");
B.this.startActivity(intent);
Intent intent = new Intent();
intent.setClass(C.this,Receiveractivity.class);
intent.putExtra("Uniqid","From_Activity_C");
C.this.startActivity(intent);
Intent Activity
//obtain Intent Object send from SenderActivity
Intent intent = this.getIntent();
/* Obtain String from Intent */
if(intent !=null)
{
String strdata = intent.getExtras().getString("Uniqid");
if(strdata.equals("From_Activity_A"))
{
//Do Something here...
}
if(strdata.equals("From_Activity_B"))
{
//Do Something here...
}
if(strdata.equals("From_Activity_C"))
{
//Do Something here...
}
........
}
else
{
//do something here
}

How to compare EditText1 of MainActivity with EditText2 of SecondActivity (SharedPreferences)?

I have 2 EditTexts. EditText1 is in MainActivity, EditText2 is in SecondActivity.
EditText1 is to login (password), EditText2 is to change password.
My code looks like this:
EditText editText1 = findViewById(R.id.login);
editText2 = findViewById(R.id.changePassword); // declared in SecondActivity
if (editText1.getText().toString().equals(editText2.getText().toString())
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "Password incorrect", Toast.Length_Long).show;
}
When I press Button to login, it shows me an error. I know it has to be initialized in a different way but how?
I tried another code with a Dialog and everything worked perfectly:
changePasswordDialog = new Dialog(MainActivity.this);
changePasswordDialog.setContentView(R.layout.activity_second_activity);
editText2 = changePasswordDialog.findViewById(R.id.changePassword);
So it works perfectly with Dialog, but how does it work without Dialog?
Try Like this
Store edit text 2 pass
String pass = editText2.getText().toString().trim();
SharedPreferences.Editor editor = getSharedPreferences(My_Prefs,Context.MODE_PRIVATE).edit();
editor.putString("pass", pass);
editor.apply();
Now Retrive the stored passwod in MainActivity
SharedPrefrences prefrences = getSharedPrefrences(My_Prefs,Context.MODE_PRIVATE);
String pass = prefrences.getString("pass","");
if (editText1.getText().toString().equals(pass)
{
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
startActivity(intent);
}
You can use intent put extra and intent get extra for this
/// you can use this as per your requirement or you can use sharedprefrence///
// In main activity ///
Intent intent = new Intent (MainActivity.this, SecondActivity.class);
intent.putExtra("editText1",editText1.getText().toString());
startActivity(intent);
/// In second activity//
String passWord = getIntent().getExtras().getString("editText1");
Log.d("password : ",passWord);

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();
}

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.

Passing data between 2 activities

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");

Categories