I am learning how to develop under Android. I've made a new project, main activity, and I wanted to design a new window. I've genereted the new Activty as it it described here
Best way to add Activity to an Android project in Eclipse?
But i can't get the visual editor for that new activity. I know that I suppose to create new layout but how to do it and connect it with that second Activity?
How to properly go back from the secondActivity(close it? minimize it? how?) to the mainActivity and don't loose information gathered why we were using secondActivity(for example what options user has made?
This is how i invoke the second Acitivity and it works fine.
Intent intent = new Intent(this,DrugieOkno.class);
startActivity(intent);
For question 1:
Here is a basic tutorial on how to create a new Activity. For a more comprehensive one containing more information about Android development you can see here.
For question 2:
For tansfering data between Activities here is a nice tutorial.
Hope that helps.
To add a new activity, follow the methods answered in this question. This way you will create a new activity without adding it to the Manifest manually. [every activity needs to be listed in the AndroidManifest.xml].
Say you create a new activity names Activity2.java. To add the new layout to the new activity, add a new xml file to res/layout folder, say activity2.xml [where you define your new activity's layout]
To link the new layout to the new activity, include this line in your newly created Activity2.java
setContentView(R.layout.activity2);
So it will look like this:
public class Activity2 extends Activity{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
}
}
2 . Now if you want to send some data from your Activity1.java to Activity2.java, you need to use Bundles.
So, if you want to send say a String from Activity1, do the following in Activity1.java:
Intent nextActivity = new Intent(this, Activity2.class);
Bundle passData = new Bundle(); //to hold your data
passDataBndl.putString("fname", fname); //put in some String. the first parameter to it is the id, and the second parameter is the value
nextActivity.putExtras(passDataBndl); //Add bundle to the Intent
startActivityForResult(nextActivity, 0); //Start Intent
To recieve data in your Activity2.java do the following (say, onCreate())
Bundle params = this.getIntent().getExtras(); //gets the data from the Intent
String firstName = params.getString("fname"); //gets value of fname
Related
I am trying to build an android app and I have a problem. First of all, I wrote a method which access your phone memory and imports an excel file which contains two columns with values that represents time and heart rate. I read the data from excel and stored it into an array list and this array list it is
DataFitbit type (DataFitbit is a class I made which contains two parameters, like excel file, time and heart rate). Now I want to create a chart with this data in other activity, but I think the code wrote in order to share my arraylist between those two activities doesn't work. I don't have any error, but when I push the button that create another activity where I want to create the chart, the app crashes.
This is the code wrote in order to share the array list between activities:
Bundle bundle = getIntent().getExtras();
ArrayList<DateFitbit> dateFitbit = (ArrayList<DateFitbit>) bundle.get("arraylist");
ListView listView = findViewById(R.id.listView);
ArrayAdapter<DateFitbit> items = new ArrayAdapter<DateFitbit>(this,android.R.layout.simple_list_item_1);
listView.setAdapter(items);
The code that opens a new activity which will contain charts is:
public void openChartActivity(){
Intent intent = new Intent(this, ChartActivity.class);
intent.putExtra("arraylist",dateFitbit);
startActivity(intent);
}
In the very first time I tried to see my data into a list view, to be sure that the share has been done, but as I said, something doesn't work well.
Do you have any suggestions for me?
You're not passing a basic String ArrayList, instead you're using a custom data type object for ArrayList.
For that to work, you need to do something as mentioned here:
1. Now, pass it in intent as:
Bundle bundle = new Bundle();
bundle.putSerializable("arraylist",(Serializable) dateFitbit);
intent.putExtra("Bundle",bundle);
startActivity(intent);
2. In the receiving activity, get it as:
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("Bundle");
ArrayList<DataFitBit> dateFitbit= (ArrayList<DataFitBit>) bundle.getSerializable("arraylist");
This will preserve your custom ArrayList model and you can then utilize it as you prefer.
I am making my own project but I am stuck.
Getting the information from the user in the first activity is working just fine.
But when I try to get those numbers, ints, in the third activity via a second activity, it is showing me the default value.
I am trying it using an Intent, but it is not working.
Intent intent = new Intent(this, active.class);
Intent intent2 = new Intent(this, BMR_Active.class);
intent.putExtra(EXTRA_TEXT_height,height );
intent.putExtra(EXTRA_TEXT_weight,weight);
intent.putExtra(EXTRA_TEXT_age,age);
startActivity(intent);
It is giving me a default value i.e. 0(I have given) in BMR activity then i am going via active activity.
Your putExtra method via intent would work fine, but are you also running putExtra BEFORE starting the third activiy?
The data is first passed from first activity to second activity, then from second activity is passed to third activity using the same method.
Additional Note:
In cases like this, I would usually use fragments instead, I would store the data in the activity and fragments will obtain the datas via getActivity(). This will eliminate the needs of multiple passing of data around.
Based on what you said in your comment about passing data straight from Activity 1 to Activity 3, then what you can do is the following:
Page1.class:
Intent toPage3 = new Intent(Page1.this, Page3.class);
int myNum = 5;
toPage3.putExtra("Number", myNum);
startActivity(toPage3);
Page3.class:
Intent receiveIntent = getIntent();
int numFromPage1 = receiveIntent.getIntExtra("Number");
I believe that this should answer your question. numFromPage1 will be an int containing the value from Page1.
I want to create an intent in MainActivity that will launch CompassActivity.
Both classes share a common layout activity_main, but use different parts of it.
Here is the intent I currently have in MainActivity which is supposed to open the CompassActivity.java class file but doesn't.
public void startCompass(View v)
{
Intent intent = new Intent(this, CompassActivity.class);
startActivity(intent);
}
What I have tried:
Altering androidmainfest.xml.
Changing the intent itself.
Changing the SDK version.
All you have to do is place the correct variables under the onCreate method and it should work fine.
Do you want to open an intent or do you just want to show/hide the different views you are using?
Instantiate your Views (findViewById) in your onCreate, then you can call view.setVisibility(View.GONE or View.VISIBILE) to hide or display your different views.
Bottom line, your views exist already on the main Activity (so no Intent needed to start another Activity) - you just need to control which views are visible at a particular time.
I am following the android programming tutorial here: http://developer.android.com/training/basics/firstapp/starting-activity.html
It details how to start a new activity and send some data to it.
I have these activities now: SplashActivity, LoginActivity, RegisterActivity, and MainActivity.
SplashActivity does a quick preference read to see if you are logged in. If you are, it passes a user ID to MainActivity thus:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(EXTRA_USERID, userID);
Where userID is a user ID number as String.
Both Login and Register Activity does its thing, which returns a valid user ID from network. They pass it to main activity using the same code snippet above.
This is defined in all three activity classes: public final static String EXTRA_USERID= "com.example.myfirstapp.USERID";.
Following the tutorial, I have this code in the onCreate method of MainActivity:
Intent intent = getIntent();
String userID = intent.getStringExtra([LoginActivity].EXTRA_USERID);
Notice the square bracketed peice of code [LoginActivity]. I don't know what to put here. I don't know which activity starts the MainActivity. Putting LoginActivity seems to only work when the login activity starts the main activity. When people register or the user ID is read from preferences in SplashActivity, the return value is an empty string.
What is the method for determining which activity started MainActivity? Or is there a way to simply get the value passed to the activity without specifying from which Activity it is created?
Any help would be appreciated.
The first argument to putExtra is a string key you define that can be used to extract the value later on. The key should start with your app's package name.
It doesn't matter where you define the key as long as you pass the right key when you launch the intent - e.g.:
public class MainActivity extends Activity {
public static final String EXTRA_USERID = "com.my.app.USERID";
}
If you launch MainActivity from LoginActivity or SplashActivity, you'll want to reference the same MainActivity.EXTRA_USERID string anywhere the intent is created. It might be easier to define a common function that takes the user ID and creates an intent with the right extra parameters.
Using Java, how can I show a Tab Widget on each and every Activity, even if that Activity is a subActivity of FirstActivity? If possible, please provide me with some code or examples.
Use THis to start the new Activity
View view = getLocalActivityManager().startActivity("tab1", new Intent(this,tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
setContentView(view);
Create Another GroupActivity and putExtra info along with your intent so when it gets it ,the group activity can check what tab should be opened.Use My code to open the new Tab.