how to open fragment from another activity android - java

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

Related

Passing string between Android Activities

I have a List Activity that when the user presses a line item, it passes the ID of the record to the next activity.
Intent intent = new Intent(CustomerListActivity.this, CustomerEditActivity.class);
intent.putExtra("CUSTOMER_ID", id);
startActivity(intent);
I can see the data in the intent when debugging on that activity; however, when I get into the next activity, the data is not coming up with the below code.
Intent i = getIntent();
Bundle b = i.getExtras();
String s = b.getString("CUSTOMER_ID");
I have debugged and poked around in the variables window, but I do not see the Customer_ID=# value as I do on the previous activity.
You should call String s = b.getStringExtra("CUSTOMER_ID");
Try this.It will work.Paste it on your next activity where you want to get data from intent.
String s= getIntent().getExtras().get("CUSTOMER_ID")+"";
This is how you can do it in a right way
//main activity
Intent intent = new Intent(getActivity(), TargetActivity.class);
intent.putExtra("ParamKey", "key_value");
getActivity().startActivity(intent);
****
//TargetActivity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target_activity_layout);
Bundle extras = getIntent().getExtras();
if (extras != null) {
if (extras.get("ParamKey") != null)
paramValue= extras.getString("ParamKey", "default_value");
}
You will have to call getStringExtra() method in New Activity to retrieve the value from Intent.
For example :
String custIdInNewAct= getIntent().getStringExtra("CUSTOMER_ID");
Bundle contains data sent by the calling activity so
Bundle b = getIntent().getExtras();
String s = b.getString("CUSTOMER_ID")`;

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 to pass an ArrayList to the StartActivityForResult activity

I need to from Activity A start an Activity B for result. I need to pass a String ArrayList from Activity A to Activity B first. I thought that this code would work but it crashes the app with a message that the list was not passed:
Activity A:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);
Activity B:
Bundle gotList = getIntent().getExtras();
ArrayList<String> listB = gotList.getStringArrayList("list");
Replace:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
Bundle sendList = new Bundle();
sendList.putStringArrayList("list",listA);
startActivityForResult(intent,2,sendList);
with:
Intent intent = new Intent(MainActivity.this,PopUpRunda.class);
intent.putStringArrayListExtra("list",listA);
startActivityForResult(intent,2);
The Bundle that is available as a parameter on startActivityForResult() is not how you pass Intent extras.

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

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