Android - Reusing Activity with a different destination Activity in Intent() description - android

I'm creating an Android exercise app - I have 10 routines with around 4-5 exercises each. Each activity has one exercise featured on it with an activity RestActivity in-between.
I wish to have the rest activity appear after each exercise, where the user can click a button which moves the app forward to the next activity. So the activity flow would be 1,r,2,r,3 - like the image.
My problem is that the destination parameter in the Intent description will be different each time, so with 50-odd transitions between activities I believe it could get pretty messy. I also want to reuse RestActivity, as my college guide had the idea of creating copies RestActivity 50-odd times with the destination being the only change (I mean really?)
I had an idea of giving every activity a key and passing through an integer at each switch.
Bundle b = new Bundle();
b.putInt("key", 1); //Your id
intent.putExtras(b); //Put your id to your next Intent
startActivity(intent);
I could use a wall of if-elses (or a switch statement) in RestActivity to work out the next Activity but I believe this would be rather messy.
Would anyone have any other ideas that would be of use? Thanks again!

Related

Android: i need to navigate to a same activity through different activities and returning back to those activities after im finished

I have two activities one is saved address activity and the other is a cart activity both have a button to navigate to the same page which is map to get the location and after finding the location their redirected to another page which is to fill their house no and after filling them it has a button when clicked I want an event to get back to their starting activities
I used intent extras to put information as key values 1 and 2 in the corresponding pages and checked it at the end but still no use
If I have understood correctly you want to send the information (location, house no) back to your starting activity?
One "dirty" solution would be like:
Intent mIntent = new Intent(mContext, YourDestinationActivity.class);
mIntent.putExtra("location", location);
mIntent.putExtra("houseNo", houseNo);
startActivity(mIntent);
But for this kind of workflow I would use Fragments
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

Saving data from Spinners, RadioGroups ,EditText, across several activities to re-use

Being very new to android I have limited experience despite having built several basic apps, so I would be grateful if you could structure any replies with this in mind.
I would like to: after a signup/login section, Collect user selections from spinners, radio buttons and text input over two or three Activities and in a fourth activity display the collected data so that it can be checked going back to make changes if need be.
When correct move on to a fifth activity where the data is structured and emailed to an email address given on the signup section. I have made the basic structure including Spinners with String arrays and have indeed made an app that works as required, passing data with extras to the next activity, however I would like to pass data to the final activity not just the next one in line. I do not really want to use a database if I can avoid it, but possibly so when all the data has been collected and reviewed, and having tried Sharedpreferences when things go wrong and the app crashes I struggle to find out why, so in an attempt to make things clearer I've stripped all that out back to basic structure leaving just working spinners, radiogroups and navigation buttons and here I am.
Being that the app structure I have left has 5 Activities and 5 class files I was hesitant to post the code as I was not sure that was the done thing, however I am willing to be guided on the question.
In your first activity, after collecting the data from the user, you can add them to your Intent using Intent.putExtras(Bundle). This is an example using a username that the user entered in an EditText.
String username = myEditText.getText().toString();
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle extras = new Bundle();
extras.putString("username",username);
intent.putExtras(extras);
startActivity(intent);
In the second activity, you can get the data from the extras in your onCreate like this:
Bundle extras = this.getIntent().getExtras();
String username = extras.getString("username");
When going to the third/fourth... activity, you can do the same steps to carry your data in the Intent you use to open the activity. First get the data from the extras, and then add them to the extras of the next intent.
You would probably want to make the variables global or because you will probably want to use them inside an onClick where they will not be accessible.

How can I pass data from one activity to another

I tried to look up something similar to what I want, but I can't find anything...
I want to pass data that I received from my database (echo json_encode($response); ) from one activity to another. The data is displayed in a listview and by clicking one item (in this case a 'category') it will take the user to second activity that displays a list of books (displayed in a listview).
My goal is to display results from received data (selected 'category' from first activity and selected 'book' from second activity) in a third activity (in here the user will have information about the book).
Any help will be great - a link to a tutorial that can help me solve this problem (well problem for me because I am new to all this), an example code or suggestions on how can I look it up...anything.
Thanks :)
You can use Intent for passing data from one activity to another activity. If you want to pass multiple types of data from one activity to another, Bundle will also be another choice for you. Look for tutorials regarding passing data through intent or bundle from one activity to another activity
Here is an Example to get you started
Intent i = new Intent(getApplicationContext(),toActivityName.class);
i.putExtra("Key","value");
startActivity(i);

Passing the user selection value from one activity to another

I am making a small calculation game.
on the main screen there would be a RadioGroup
o Easy o Medium o difficult
Button Continue
then after the user selects this and presses continue then on next page another radio group
o Addition o Subtraction
Button Start
Now I want to display questions depending on the selection of radio buttons from there 2 activities.
How can I do that, for a single radio group I can use changeListener...But here I have to consider values from 2 radio groups simultaneously.
So how to do that. I tried my best to explain this using example and representation too.
Thanks
Use a bundle with Intent extras to pass data between activities.
Intent i = new Intent(getContext(), SecondActivity.class);
Bundle b = new Bundle();
b.putExtra("key", value);
i.putExtras(b);
startActivity(i);
http://developer.android.com/reference/android/content/Intent.html#putExtras(android.os.Bundle)
and in your SecondActivity
getIntent().getStringExtra("key");
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
For the apps that I've been involved with I've been using a application object to store state variables that need to be visible in more than one activity. The application object gets created before any activity objects, and is the last thing to be destroyed when the application is closed, so it's a good place to store state variables. You're allowed one application object per app, and the class type needs to be declared in the manifest. It sounds like your difficulty variable and your add/subtract are state variables, so I'd suggest you go down this path.
You may also want to use the SharedPreferences if you want to save the value longer the current instance.
See for more details.
http://developer.android.com/guide/topics/data/data-storage.html#pref

Using Primitive Data Types in another Class and the res/menu/.xml file

I'm a very new to Java. I thought I was doing okay but have now hit a brick wall, so to speak.
The idea is I use the 'home button' '_menu' for the user to choose one of 26 formats. The format they choose has 3 variables. Those 3 variables are then used in the first xml view, along with 2 user inputs to calulate another variable. This variable is then used in a second xml view, along with a third user input here, to calculate the final answer.
I have created the 26 choices and if for example I choose option 5, on the emulator screen I see all the correct values associated with this choice. I don't think those values are getting stored anywhere. I say this because if I come out of that view and return back into it, it's not showing, as in my example, choice 5. It's showing its initial state, as though I was going into it the first time. I assume it's something to do with launching this activity from the start but is there anyway around this. Or really where do I start.
My second question is with the integer variables that I created from this choice. I need to pass them into another java file for the first set of calculations. I've tried to pass the variables/data with the, 'new intent putExtra' but can't get it to work. But I don't think I can use this anyway since the I don't want to launch the second view directly from the res/menu/ .xml view.
Not sure if this is making sense to anyone. Can someone help point me in the right direction?
I don't quite understand your first question, but it sounds like you're launching a new activity and when you quit and come back to it, everything is reset. If you're launching a new activity after selecting the options from your phone's menu button, you should implement a method that saves data to the shared preferences of the main activity. This method should be called on the activities onPause(), onDestroyed(), or onStop() method. You can also add a method on onResume() where the activity checks if there's any data stored in shared preferences and if so, make the desired changes.
As for your second question...I kinda don't understand it either. new intent and putextra is used when you're starting a new activity and want to pass data to it. Views are not "launched" they are only inflated and brought on display whenever you want. I did an app once where I had everything in one activity and just using setContentView() method all the time. In the long run, it just complicated everything. It is easier to keep things simple and launch activities. Here is an example of some variables being passed to a new activity:
On my main activity (FirstActivity) I have:
(when newActivityButton is clicked)
case R.id.newActivityButton:
Intent mIntent = new Intent(FirstActivity.this,SecondActivity.class);
String[] luckyNumbers = {
luckyNumber[0].getText().toString(),
luckyNumber[1].getText().toString(),
luckyNumber[2].getText().toString(),
luckyNumber[3].getText().toString(),
luckyNumber[4].getText().toString(),
luckyNumber[5].getText().toString()};
mIntent.putExtra("luckyNumbers", luckyNumbers);
mIntent.putExtra("message", messageField.getText().toString());
FirstActivity.this.startActivity(mIntent);
break;
luckyNumbers[] is an array of textviews.
Then on my NewActivity onCreate(), I have:
message = getIntent().getExtras().getString("message");
Log.i("TAG", message);
luckyNumbers = getIntent().getExtras().getStringArray("luckyNumbers");
cv.setLuckyNumbers(this.luckyNumbers);
cv.setMessage(this.message);
where cv is just a custom view I created with its own methods

Categories

Resources