I'm trying to pass class name with extra, how to do that ?
Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra(" ",StartScreen.this);
I want to get the class name in Setting class but I don't want it to be String cause I'm going to use this class name like that :
Bundle extras = getIntent().getExtras();
extras.getString("class");
Intent i = new Intent(Setting.this, class);
startActivity(i);
you can use this code
Intent p = new Intent(StartScreen.this, Setting.class);
p.putExtra("class","packagename.classname");
and in setting class
Bundle extras = getIntent().getExtras();
String classname=extras.getString("class");
Class<?> clazz = Class.forName(classname);
Intent i = new Intent(Setting.this, clazz);
startActivity(i);
A tidier way than the accepted answer would be to use Serializable or Parcelable.
Here is an example of how to do it using Serializable:
In your first activity...
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("EXTRA_NEXT_ACTIVITY_CLASS", ThirdActivity.class);
startActivity(intent);
Then in your second activity...
Bundle extras = getIntent().getExtras();
Class nextActivityClass = (Class<Activity>)extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS");
Intent intent = new Intent(SecondActivity.this, nextActivityClass);
startActivity(intent);
Doing it with Parcelable is pretty much the same, except you would replace extras.getSerializable("EXTRA_NEXT_ACTIVITY_CLASS") in the above code with extras.getParcelable("EXTRA_NEXT_ACTIVITY_CLASS").
The Parcelable method will be faster, but harder to set up (as you need to make your third Activity implement Parcelable - see http://developer.android.com/reference/android/os/Parcelable.html).
Related
I wanted to get the name of pizza on bill page put it isn't displaying(blank).
Menu.java:
bundle = new Bundle();
bundle.putString("name",pizzaName);
y = new Intent(this,Bill.class);
y.putExtras(bundle);
startActivity(y);
Bill.java:
text1=(TextView)findViewById(R.id.textView6);
y= new Intent();
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();
Output:
You do not need to do y= new Intent(); in Bill.java
Change code in Bill.java as follows. Also, for better naming convention, change Bill.java to BillActivity.java
TextView text1 = (TextView) findViewById(R.id.textView6);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String name = bundle.getString("name");
text1.setText(name);
}
You can simply pass and retrieve value like this.
// Creating and initializing an Intent object
Intent intent = new Intent(this, NextActivity.class);
// Attach the key value pair using putExtra to this intent
String pizza = "Margherita";
intent.putExtra("PIZZA_NAME", pizza);
// Start the activity
startActivity(intent);
// Get the current intent
Intent intent = getIntent();
// Get the attached extras from the intent
// We should use the same key as we used to attach the data.
String pizza = intent.getStringExtra("PIZZA_NAME");
Remove y= new Intent(); in your bill.class
No need to get y= new Intent(); when you get data from bundle
text1=(TextView)findViewById(R.id.textView6);
bundle=getIntent().getExtras();
Name=bundle.getString("name");
text1.setText();
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();
}
I am attempting to pass an int through an intent to another class and have managed to successfully pass through the integer however I am unsure as how to convert a Bundle to an Integer.
Code from Intent:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class).putExtra("totalTime", totalTime);
startActivity(intent);
}
Code in Timer class:
Bundle time = getIntent().getExtras();
if(time == null)
{
timeDisp.setText("Failed.");
}
else
{
totalTimeMs = Integer.parseInt(String.valueOf(time));
timeDisp.setText(totalTimeMs);
}
Thanks in advance :)
Intent can hold directly all java primitives types and parcelable/serializable objects.
You may have a confusion that it can also hold Bundles.
Do you really need to put your integer in a Bundle? It can be true for multiple values that logically coupled.
Check Intent API.
If your totalTime is of type int which you pass through putExtra() the you can use:
int time = getIntent().getExtras().getInt("totalTime");
You are not adding your intent to a Bundle, so on the recieving activity you are trying to get data out of an empty Bundle.
You would Add the data to a bundle by:
private void nextPage()
{
Intent intent = new Intent(this, Timer.class);
Bundle b = new Bundle():
b.putString("totalTime", totaltime);
intent.putExtras(b);
startActivity(intent);
}
Then you would retrieve the String from the bundle:
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String time = extras.getString("totalTime");
I am trying to pass the list of object from one activity to another. But not able to send it.
Here is my code.
setPrefBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent homePageIntent = new Intent(SetPreferences.this,
KlarityHome.class);// Connecting setPreferences page
// with Klarity Home Page
List<PracticeVO> tempList = new ArrayList<PracticeVO>();
tempList.add(practiceObj);
homePageIntent.putExtra("SelectedPractice", tempList.toArray());
startActivity(homePageIntent);
}
});
And retriving in 2nd activity like this :
Intent prefIntent = new Intent();
List preferencedPractices = (List) getIntent().getExtras().get("SelectedPractice");
ArrayAdapter<PracticeVO> praticeAdapter = new ArrayAdapter<PracticeVO>(this, android.R.layout.simple_spinner_item,
preferencedPractices);
praticeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
practiceSpin.setAdapter(praticeAdapter);
You need to use putExtra() function to pass values to another activity
Intent newIntent = new Intent(MainActivity.this, ResultActivity.class);
newIntent.putExtra("yourTxtField",yourTxtField.getText());
to receive you need to use other getIntent and Bundle.
Intent myIntent = getIntent();
Bundle valores = myIntent.getExtras();
//Objeto que vai calcular os resultados
Resultado resultado = new Resultado();
//Recupera os valores recebidos pelo intent
double seuDinheiro = Double.parseDouble((String) valores.get("seuDinheiro").toString());
To pass objects see this SO post
How do I pass an object from one activity to another on Android?
How to pass an object from one activity to another on Android
Try in this manner
ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);
In the Transfer.class
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");
What type is PracticeVO? In order to do what you are intending, PracticeVO needs to implement the Parcelable interface (http://developer.android.com/reference/android/os/Parcelable.html). Then you can use this version of the putExtra() method: http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Parcelable[]).
Notification fundinProgram = (Notification) getIntent().getSerializableExtra(DETAIL_OBJECT);
implement Serializable in your PracticeVO class file
When sending from an activity:
List<PracticeVO> PracticeVO = new ArrayList<PracticeVO>;
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("PracticeVO", ArrayList<PracticeVO> PracticeVO);
startactivity(PracticeVO);
At the receiver side:
List<Question> questions = new ArrayList<Question>();
questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");
source: Link
I was able to achieve it this way:
Make your model class (PracticeVO) as well as your Activity class (KlarityHome) implement Serializable.
public class MyClass implements Serializable {...}
In your activity class, store your objects in an ArrayList rather than a List.
Finally, you can do this:
// create intent
Intent homePageIntent = new Intent(SetPreferences.this, KlarityHome.class);
// create your ArrayList and add your item(s)
List<PracticeVO> tempList = new ArrayList<PracticeVO>();
tempList.add(practiceObj);
// add your arraylist to your intent
homePageIntent.putExtra("SelectedPractice", tempList);
// go go go ;-)
startActivity(homePageIntent);
I hope this helps. Merry coding!
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?