Friday, 31 August 2012

Passing Multiple Data Between Activities In Android

If you have problem passing data or multiple data between activities in android application, then this post will solve your doubt. In Android application development process, it is easy to call an activity from another activity using an intent. But what if needed to pass data from one activity to another activity. One can do this in different way for e.g. using file system, preferences, bundle and so on.

Let us consider one example which have two activity class:

i.e. FirstActivity.java and SecondActivity.java.

Here we need to send data from FirstActivity.java to SecondActivity.java.

In FirstActivity.java

Create an Intent to call SecondActivity.java and use putExtra(key,value) method to pass data to SecondActivity.java

int id = 1;
Intent i = new Intent(this,SecondActivity);
i = putExtra("passId",id);
startActivity(i);
finish() ;
"passId" is key and "id" is value.

In SecondActivity.java

Use getExtra(key) method to get passed data from FirstActivity.java

int id;
id = getIntent.getintExtra("passId",0)
"passId" is key and 0 is default value i.e. if no data is passed it will assign 0.
For multiple data one can also use Bundle:

Bundle: Bundle are generally used to pass data between activities. Using bundle one can pass all type of values between activities.

In FirstActivity.java

Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userName", "Name");
bundle.putString("userPwd", "Password");
i.putExtras(bundle);
startActivity(i);

In SecondaActivity.java

Bundle getData = getIntent().getExtras();
String name= getData .getString("userName");
String pwd= getData .getString("userPwd");

Check out example of passing data between activities at Android Quiz Game App Source Code

For more info refer link given below:

Thursday, 23 August 2012

Solved R Java File Missing In Eclipse

For the beginner, who are working on eclipse to develop android application, the big problem are error's. Likewise when I was creating android app I got stuck at one error which is more common to all developer i.e
 
R.java file missing in package explorer in eclipse.
 
I searched for the solution online and got many answer but none of worked. I then checked once again the project carefully and found the error which solved ma problem.

(click on image to enlarge)

R.java file went missing in package explorer in eclipse when  cleaned project after few changes.
 
Tried all possible solution found on web but of no use. Then checked error message on console screen and sensed the problem.

Problem with res folder. Layout XML file name validation problem. File name should be in lowercase and I used first character in uppercase.
 
After renaming the file in lower case, cleaned again project and problem solved.
 
Note: There may be other solution for this problem depending on error type.




 

About CodingAndError

Brilliant mind create Brilliant Innovative. CodingAndError blog is created by a man who dream's that "Knowledge Should Be Shared Not Sold". In this blog one can find many informative article without any cost. All content here are best to author knowledge. CodingAndError will not be responsible for any damage or harm done by any of its content.