The issue I encountered is really interesting for me. I develop an application using the Master Details template. On tablet everything works fine, but when switch to phone here I get something that I am not sure how to approach. After I select an item I launch an intent with the following code:
Intent detailIntent = new Intent(this, CategoryDetailActivity.class);
detailIntent.putExtra(CategoryDetailFragment.ARG_ITEM_ID, id);
startActivity(detailIntent);
and the problem is that I can't get the value of that id from the destination fragment:
if (getArguments().containsKey(ARG_ITEM_ID)) {
_locations = _sqlLoc.getAllLocationsByCategory(getArguments().getInt(ARG_ITEM_ID, -1));
}
else{
_locations = _sqlLoc.getAllLocationsByCategory(-1);
}
With this code I always get -1 instead of the real value. Can someone help me with this issue?
Why do you start a new Activity? You should just replace your current Fragment with the new one if you want to implement the phone version of Master-Details pattern correctly. Something like this: getFragmentManager().beginTransaction().replace(R.id.main_layout, new DetailsFragment()).addToBackStack(null).commit();
You can store any data you need in Activity and get it via (MyActivity)getActivity().getData() from your fragments. This way you don't need to pass any data via Bundle at all.
Related
First of, I'm a noob in Android development....
So, my problem is - I have a Activity with a EditText view, so someone can put his name in it.
And what I want to do with that input - I want it to be displayed in all activities in TextViews where it needs to be.
Example - Like in a book -> Input name: What are you doing kid? Kir: "I don't know..." Input name: "Just don't do it anymore..."ect.
And then in the next Activity by clicking a Button, the same, I want that same input name to display in ALL ACTIVITES , TextViews where it needs to be, but I don't know how to declare it, where to declare it, what code ect.
I hope you all will understand what i mean.
And thank you for your support.
One way is by passing data into intents before starting the activity.
Intent intent = new Intent(context, MyActivity.class);
intent.putExtra("key_name", "John Cena");
context.startActivity(intent);
Then retrieve the data in MyActivity by:
Bundle extras = getIntent().getExtras();
String name = extras.getString("key_name"); // name should be John Cena
Simillar functionality also exists for Fragments.
Basically you wants to Preserve the Value\Data. For such scenarios, it is best to use [Preferences] where you can save your data on disk in a private file and read it anytime and anywhere.
Android Preferences
I'm currently developing an android application that makes use of Google Places API.
I have 2 activities that I want to pass a variable between. I have tried passing by Intent using putExtras and getExtras.
I have been having issues with the getExtras. I have debugged my code, used breakpoints and an output of the variable to the log.
Here is my code for the activity of which I want to pass the variable from:
Intent mapIntent = new Intent(getBaseContext(), MapsActivity.class);
Spinner distanceSpinner = (Spinner) findViewById(R.id.spinner_distance);
String radius = distanceSpinner.getSelectedItem().toString();
mapIntent.putExtra("radius", radius);
Log.e("Passer", String.valueOf(radius)); //Shows that value is not = null.
startActivity(mapIntent);
The log out put for this class is correct and displays the variable.
Here is the code for my class that I want to pass the variable to:
Intent filterIntent = new Intent(getBaseContext(), LocateFilterActivity.class);
Bundle bd = filterIntent.getExtras();
String radius= (String) bd.get("radius");
Log.e("TESTER", String.valueOf(radius));
startActivity(placeIntent);
The log does not actually output anything for this class. I know it can reach and execute the line of code through debugging.
I have tried coding this several different ways following previous SO questions/answers but not having much luck.
Any help would be much appreciated :)
You can access data which were passed to an activity with getIntent().getExtras() method which you can call in a newly created activity that you have created with startActivity(intent) call. :)
Intent myIntent = new Intent(this,Q1.class);
In my app I have 1000 different activities namely Q1,Q2,Q3....Q1000.
I have a button on each activity and I want when a user clicks on it, he should land on some random activity.
How to achieve this ?
Although this is terrible, I'll still show a possible way to do this. But just remember, this is TERRIBLE.
You can store all the activity classes in an ArrayList.
ArrayList<Class<Activity>> activities = new ArrayList<> ();
And then you add all the activities into the ArrayList. Yeah, I know, this part is tedious. For example,
activities.add (Activity1.class);
And then you create a Random called rand and use that to access an element in the list:
list.get (rand.nextInt (list.size()));
Here is another "better" way to do it, but it's still kinda bad. I strongly advise you to store teh questions in a database. Anyway, here's the better-but-still-bad method.
You create a question class:
public class Question {
//here you can put correctAnswer, questionText etc
}
After that, you make an ArrayList of Questions i.e. ArrayList<Question>.
ArrayList<Question> questions = new ArrayList<> ();
And still, you need to add 1000 questions to the array list. Then you can just use a Random to access it.
When you want to display a question in one activity, you can just putExtra in Intent before starting the activity. putExtra basically passes some "parameter" thingys to the activity. Then in the activity, you can just get the "Extra" and use the Question object to display. e.g. set the text of a TextView to the question text.
this can be also used:
try {
Random rand=new Random();
Intent i = new Intent(this, Class.forName("test.hu.test.Q"+rand.nextInt(1000)+"")); // get activity's class by reflection
startActivity(i);
}catch (Exception e)
{
}
But i also suggest to use DB.
am trying to pass an arrylist from one intent to another.
My code is as follows:
intent.putExtra("near_places", nearPlaces);
intent.putExtra("nearby", nearbylist);
and at the new intent i do this:
nearPlaces = (PlacesList) extras.getSerializable("near_places");
nearbylist = (ArrayList<FsqVenue>)extras.getArrayList("nearby");
the code for the firat is correct but for the second it shows me errors where fsqVenue is a class
please help me
first make sure FsqVenue class implements Serializable interface then use this code for getting ArrayList in other Activity :
nearbylist = (ArrayList<FsqVenue>)extras.getSerializable("nearby");
In your receiving intent you need to do:
Intent i = getIntent();
near_places = i.getStringArrayListExtra("nearPlaces");
that's perfect work please try and give replay.
Princewill, here is your answer :- I want to let user add multiple items by action sequence, i have done this and looking for to make it more advanced.....JUST OPEN LINK AND VIEW code for Catalogue and SingleMenuItem Activity and done.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have been banging my head across the table for days trying to figure this out. I'm new to both Java and the Android platform and I am writing my second application. In a nutshell, I want the user to enter in some information in the first activity. That activity then takes those inputs and runs a series of mathematical computations and then displays the results in several editTexts in a separate activity. In my mind, the application takes the input, runs the computations and then stores them in some variables. The variables then need to be passed to the second activity which then displays the results. I have most of the application coded and I can get it to work properly if I keep the inputs and displayed results in one activity. Any Ideas on how I can get the displayed results to show up on another activity? Any direction here is much appreciated.
This can be done with use of intent. one of the use of Intent is to pass the data between activities. In your scenario what you need to do is
STEP 1
After taking input from the user, do computation, store result in
the variables
bundle that in the intent which you are using to
start next activity. You can achieve this by below code
Intent intent = new Intent();
intent.putExtra("KeyForResult1", <your variable>);
intent.putExtra("KeyForResult2",<your variable>);
startActivity(this, nextactivity.class);
in the nextactivity you need to get the intent and extract the values in the variable
which can be achieved
variabletype variable = getIntent().getExtras().get("KeyForResult1");
variabletype variable = getIntent().getExtras().get("KeyForResult2");
You can use this:
ActivityOne.class:
//compute the data and get the result here
//suppose results are,
int resultInt=24;
String resultString="abc";
Intent intent=new Intent(getApplicationContext(),ActivityTwo.class);
intent.putExtra("ResultInInteger",resultInt);
intent.putExtra("ResultInString",resultString);
startActivity(intent);
this will open ActivityTwo.class,where you can get the data like:
int resultInt=getIntent().getIntExtra("ResultInInteger");
String resultString=getIntent().getStringExtra("ResultInString");
I was able to figure it out with a mixture of data I had researched. It seems what finally worked was
int resultInt=24;
Intent myIntent = new Intent(CalPalActivity.this, ResultsActivity.class);
myIntent.putExtra("intVariableName", resultInt);
startActivity(myIntent);
in the first activity and
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
in the second. Why it worked like this and kept giving me errors such as "the method getIntExtra(String, int) in the type Intent is not applicable for the arguments (String)" in the second activity I'll never know. Even the Quick-fixes didn't fix it (I just kept getting new errors). All in all, it's an expensive (5 hour +) but well learned lesson. All of your help was much appreciated. I was able to understand and learn a great deal more about the language. Thank you!
You should read up on Intents. You can store extras in an intent, much like URL parameters. If you use that intent to start your next activity, it will be able to extract the data from the intent's extras.
There are several options. For your task I'd choose passing the data in Intent's extras.
For all options read this
Here is an option.
In first Activity:
Intent yourNextActivityIn=new Intent(this,yourNextActivity.class);
yourNextActivityIn.putExtra("tag",valueToBePassed);
startActivity(yourNextActivityIn);
In second Activity:
//If value is an Integer with default value 1
int value = getIntent().getIntExtra("tag", 1);
else
//if value is a string
String value = getIntent().getStringExtra("tag");