I'm making Sudoku game. There are two classes: difficulty and Screen class. When the 'easy' button is pressed, I want to generate a random number from 1 to 9, and that numbers should go to the Screen class layout.
My 'easy' button is in choosedifficulty class layout.
You can pass information between activities using Intent extras. In your case, you could do (assuming I understood what you wanna do):
public void startSudoku(int chosenDifficulty) {
Intent i = new Intent(this, // Your current activity
SudokuActivity.class); // The activity showing the Sudoku
i.addExtra("com.example.sudoku.DifficultyLevel", chosenDifficulty);
startActivity(i);
}
Then, in the onCreate method of your Sudoku activity, you can get the difficulty level:
private int difficulty = 0;
protected void onCreate(Bundle savedInstanceState) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
difficulty = extras.getInt("com.example.sudoku.DifficultyLevel", 0);
}
}
Be carefull cause a sudoku is not only "random numbers from 1 to 9", so I recommend you to implement the algorithm properly. (Take a look at http://en.wikipedia.org/wiki/Sudoku_algorithms).
About passing data from one activity to another, you can use the intent. For example:
//In the place you launch your game
Intent myIntent = new Intent(myContext, Screen.class);
//sudoku is a String, for example, that contains the sudoku you want to pass
myIntent.putExtra("sudokukey", sudoku);
startActivity(myIntent);
And then, to retrieve the data:
Bundle bundle = getIntent().getExtras();
String mySudoku = bundle.getString("sudokukey");
Related
I have an activity which can be accessed via different activities.
Like you have one activity containing a listview and the second containing a gridview and both shows the same data. When you click on an item a new activity with some details is shown. I need to somehow remember which activity was the initial one (with gridview or listview) so that I can set a button to redirect here. But it's not enough to just return to previous activity (like using finish() to close the current one), because there is a way to navigate among different objects from inside the details activity (I have a gridview on that screen). So I need to remember the initial view during moving through the details activity for various number of times.
Is there a way?
EDIT: omg why so many downvotes? At least tell me why it is so stupid, I'm learning coding for Android for 2 weeks how am I supposed to know everything??
This sounds like it would best be solved by using two Fragments within the same Activity
You can use a bundle object to pass data to the new activity (B) so you could know wich activity (listView or gridView) had started it.
In activity B:
private static final String ACTIVITY_TYPE = "activity_type";
public static Intent createIntent(Context context,String activityType)
{
Intent intent = new Intent(context, ActivityB.class);
Bundle b = new Bundle();
b.putString(ACTIVITY_TYPE,activityType);
intent.putExtras(b);
return intent;
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
String activityType;
if (b != null)
{
activityType = b.getString(ACTIVITY_TYPE);
}
//the calling activity didn't use the createIntent method and didn't provide the activity type
else
{
activityType = "some_default_type";
}
//save activity type to use later
//rest of your code
}
In the calling activity:
Intent intent = ActivityB.createIntent(ActivityListView.this,"activity_list_view");
startActivity(intent);
have total 3 activites. I pass the data from the first activity like this:
Intent intent = new Intent(getActivity(), Movie_rev_fulldis_activity.class);
intent.putExtra("mov_pos", position + "");
startActivity(intent);
this working fine all data visible to my second activity but i want to display
one filed item to third activity when i click second activity image
here my second activity
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
// Toast.makeText(Movie_rev_fulldis_activity.this,
// youtubeLink, Toast.LENGTH_LONG).show();
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
Toast.makeText(Movie_rev_fulldis_activity.this,
Reviews_update.revData.get(mov_pos).getYoutubeLink(), Toast.LENGTH_LONG).show();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
in second activity i am getting values but when i send one filed to third activity that value passing null any one please help me i stuck in their,
I want to show YoutubeLink field sencnd activity to third activity how to parse that any one please help
In FastActivity:
Intent in = new Intent(FastActivity.this, SecondActivity.class);
in.putExtra("a", yourdata);
startActivity(in);
In SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String a = extras.getString("a");
}
You can also use any static variable and use it in any class
static int a = 5;
And in any other class
int b = classname.a;
classname is the class where static variable is declared.
Make that variable static and then pass
Your variable object reference get change in the THIRD Activity so, it returns null
This question already has answers here:
keeping a variable value across all android activities
(5 answers)
Closed 9 years ago.
im having a problem in passing information from one activity to another ... the idea is to choose from a vegetable market what do you need and choose the quantity this pic explain
Activity 1
i want to take all the information from the first activity so when i choose banana for example then put the quantity click on add to shopping cart the next activity would be like this
Activity 2
so how code i take the information from the first activity to the other ?!
When you start a new activity, you can send a bundle with it. Bundles can contain data like strings, booleans, ints, pretty much anything.
For example:
Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("value_1", value1);
bundle.putInt("value_2", value2);
intent.putExtras(bundle);
this.startActivity(intent);
And in your second activities onCreate method, you can get the values from the bundle like:
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
Bundle bundle = getIntent().getExtras();
String value1 = bundle.getString("value_1");
String value2 = bundle.getString("value_2");
First associate EditText to a variable:
`EditText bananas = (EditText)findViewById(R.id.bananastext);`
Then get value of it:
`int value = Integer.parseInt(bananas.getText().toString());`
Then create an Intent and attach on it value:
`intent.put("bananas",value);`
Then from the other activity create a Bundle and get value:
Bundle extras = getIntent().getExtras;
Int value = extras.getInt("bananas");`
Hi everyone.
I'm new in android and I'm working on an app in which I need to recall the same activity to enter the information of a variable amount of entities (passengers).
I have the following:
btnContinue3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for (int i=0; i<Pssngr; i++){
passenger[i] = new
Intent(getApplicationContext(), Pasajeros.class);
startActivity(passenger[i]);
}
}
});
Pssngr is the amount of passsengers or entites that need a unique activity to get their information entered.
The trigger is the button then the activities will be called one by one following an array
I try this code but after clicking on the button the app crashed.
Please someone help me find a way to make this work.
thanks
It crashes because You are trying to start x number of Activities at once.
If You have to run new Activity for each of Passengers best in this scenario will be startActivityForResult
I beliver effect You trying to get is that user clicks on button just once and activities for each passenger will open one after another.
To do it in method onClick You will start only first activity, don't use loop.
You start consequently next activities in onActivitiyResult
In addition to what Gustek mentions above, a better way to approach this would be to have one activity and just pass the different values from the parent (PassengersAvitivty) activity through the intent as below:
final Intent intent = new Intent(PassengersAcitivty.this, PassengersEntityActivity.class);
intent.putExtra("PASSENGER_FIRSTNAME", passenger[i].getName());
intent.putExtra("PASSENGER_LASTNAME", passenger[i].getLastName());
intent.putExtra("PASSENGER_EMAIL", passenger[i].getEmail());
startActivity(intent);
and here is how you can retrieve them on your activity (PassengerEntityActivity)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
firstname = extras.getString("PASSENGER_FIRSTNAME");
lastname = extras.getString("PASSENGER_LASTNAME");
email = extras.getString("PASSENGER_EMAIL");
}
else
{
//Log.v("NO VALUE", "OOPS");
}
I can clarify further if needed.
Okay, so this far I made my app display an activity and can link to different activities based on click cases. Now, my question would be, how do you populate the opened activity with data that corresponds with the data in the item of the listview?
Easy said,
ListView (for ex. 10 items)
On click, opens ContentViewer activity
void ContentViewer::onCreate() {
setContentView contentviewer(xml);
}
(contentviewer has different textviews and imageviews with diff IDs.)
Now, when contentviewer is opened by clicking case 0 (first item in listview), then data 0, image 0 and so on.
Any ideas?
Essentially you're looking at way to pass params to your second activity from the first? Here's how:
Activity1.java:
Intent intent = new Intent(this, Activity2.class);
intent.putExtra(ReportActivity.REPORT_TYPE, reportId);
startActivity(intent);
Activity2.java:
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
if (intent != null) {
Bundle bundle = intent.getExtras();
if (bundle != null) {
int reportId = bundle.getInt(REPORT_TYPE);
}
}
The idea here to to put name/value pairs into your Intent from the calling activity and in the called activity you read the name/value pairs off of the invoking intent. In the example above I am passing int(reportID) to the calling activity. You can pass any other primitive type to it. If you're looking to pass custom objects, you'll need to implement Parcelable.