transfer data between two activity - android

I want to send a data between two activities and based on data I show one view (I have a frameLayout and based on that data I commit a fragment for fill frameLayout). I write this code but I think it is not transferring data between the two activities! because always case 0! is executing
What is the problem??
splash:
private OnClickListener onClickListener=new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("circuleProduct", "clicked");
Intent intent;
switch(v.getId()){
case R.id.btnCircleProduct:
intent=new Intent(Splash.this,MainActivity.class);
startActivity(intent);
intent.putExtra("value",1 );
break;
case R.id.btnCircleIntroduce:
intent=new Intent(Splash.this,MainActivity.class);
startActivity(intent);
intent.putExtra("value", 2);
break;
case R.id.btnCircleContact:
intent=new Intent(Splash.this,MainActivity.class);
startActivity(intent);
intent.putExtra("value", 3);
break;
case R.id.btnCircleMore:
intent=new Intent(Splash.this,MainActivity.class);
startActivity(intent);
intent.putExtra("value", 4);
break;
}
}
};
Activity:
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int value=0;
Intent intent=getIntent();
value=intent.getIntExtra("value", 0);
switch(value){
case 0:
ft.add(R.id.frameLayout, new Introduce());
ft.commit();
break;
case 1:
ft.add(R.id.frameLayout, new Product());
ft.commit();
case 3:
ft.add(R.id.frameLayout, new Contact());
ft.commit();
case 4:
ft.add(R.id.frameLayout, new More());
ft.commit();
break;
}
}
}

DO put extra before startActivity
intent=new Intent(Splash.this,MainActivity.class);
intent.putExtra("value", 3);
startActivity(intent);
Also give break in case statement, in switch case statement you are missing 2
switch(value){
case 1:
ft.add(R.id.frameLayout, new Introduce());
ft.commit();
break;
case 2:
ft.add(R.id.frameLayout, new Product());
ft.commit();
break;
case 3:
ft.add(R.id.frameLayout, new Contact());
ft.commit();
break;
case 4:
ft.add(R.id.frameLayout, new More());
ft.commit();
break;
}

try to use data transfering between two activities in this way
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", 1);
startActivity(i);
and in other activity after oncreate()
Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
see this detailed tutorial on Android Intents

Do the intent.putExtra("value", ...); before you start the activity.

Do this
intent.putExtra("value",1 );
startActivity(intent);
instead of
startActivity(intent);
intent.putExtra("value",1 );

in main activity you have forgotten the break in switch. That means commit will be called several times. Add break to case1 and case3.
And also add the extras before calling startactivity

Related

Use Activities instead of Fragments in a Sliding Menu

I have a sliding menu with some Fragments, but i want to change it with Activities, is it possible?
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new X();
break;
case 1:
fragment = new Y();
break;
case 2:
fragment = new Z();
break;
default:
fragment = new X();
break;
}
if (null != fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
I don't know how to change the last part of the code. --> FragmentManager and --> FragmentTransaction. Thanks!!
Yes you can use Activity in place of fragment, But
you have following challenges.
You need to implement sliding drawer on every activity, if you uses fragment then HomeActivity sliding drawer will be shown on all fragment
update your code like this if you want to implement activity
private void replaceFragment(int pos) {
Intent intent = null;
Context context = this;
switch (pos) {
case 0:
intent = new Intent(context, SecondActivity.class);
break;
case 1:
intent = new Intent(context, ThirdActivity.class);
break;
case 2:
intent = new Intent(context, FourthActivity.class);
break;
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
if (intent != null) {
startActivity(intent);
}
}

How to prevent for recreate activity when navigation drawer menu click

In my application i don't want to recreate activity on navigation drawer menu click when i am on same activity.
user on map activity after navigation drawer item click map reloading. i don't want to reload map or refresh activity.
private void displayView(int position)
{
switch (position)
{
case 0:
break;
case 1:
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
break;
case 2:
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
break;
case 3:
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
break;
default:
break;
}
You can add the following to your function
private int current=-1;
private void displayView(int position)
{
switch (position)
{
case 0:
current =0;
break;
case 1:
if(current!=1){
current =1;
Intent intent1 = new Intent(this, Activity1.class);
startActivity(intent1);
//finish();
}
break;
case 2:
if(current!=2){
current=2;
Intent intent2 = new Intent(this, Activity2.class);
startActivity(intent2);
finish();
}
break;
case 3:
if(current!=3){
current=3
Intent intent4 = new Intent(this, Activity3.class);
startActivity(intent4);
finish();
}
break;
default:
break;
}

Navigation drawer with activities instead of fragments

What i have is left navigation menu that i have made using navigation drawer and it works just fine with fragments , and i have five buttons each one open a fragment but now i want each case to open an activity instead of fragment , and i have tried to do it using intent but it didn't work !!
here is my code :
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
//fragment = new HomeFragment();
// Intent i=new Intent(MainActivity.this,MainActivity.class);
// startActivity(i);
break;
case 1:
//fragment = new FindPeopleFragment();
break;
case 2:
//fragment = new PhotosFragment();
break;
case 3:
//fragment = new CommunityFragment();
break;
case 4:
//fragment = new PagesFragment();
break;
case 5:
//fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
So what shall i change in this code so it can open an activity instead of fragment??? can anyone help me?
You need to change your function to load a class Fragment in some case and in other cases a class Activity
case 0:
activity = new MyActivity();
fragment = null;
break;
case 1:
fragment = new MyFragment();
activity = null;
break;
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, (android.app.Fragment) fragment).commit();
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}else {
if(activity != null) {
Intent i1 = new Intent(MainActivity.this, activity.getClass());
i1.putExtra(EXTRA_MESSAGE, position);
startActivity(i1);
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
Could you please post the Error from Logcat:
What i would check first is:
Are the Activities declared in the Manifest.xml as Activies?
Secondly check if you calling Activities with an Intent, and not Fragments
This is how you start Activities correctly.
Intent intent = new Intent(mActivity, class1); // class1 The class to open
startActivity(intent);

Send float from activity to activity

I'm beginning android developpement and i don't why my code is not working. The aim is simple : I have a main activity, a menu and a second activity. I want to send a float value from the main to the second activity and .. it's not working !
Here is my code from the main :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And the StatsActivity.class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats_main);
Intent intent = getIntent();
float consumOn = intent.getFloatExtra("consumOn", 0);
float consumOff = intent.getFloatExtra("consumOff", 0);
EditText editTextA = (EditText)findViewById(R.id.editText1);
editTextA.setText(String.valueOf(consumOn), TextView.BufferType.EDITABLE);
EditText editTextB = (EditText)findViewById(R.id.editText2);
editTextB.setText(String.valueOf(consumOff), TextView.BufferType.EDITABLE);
}
When I launch my code, it stills 0 and not a 4 and 5. Any ideas ? Thx.
Replace the getFloatExtra with
Bundle bundle = getIntent().getExtras();
float yourFloat = bundle.getFloat("key");
I'm going to go out on a limb here and say that maybe the putExtra is being interpreted as int rather than a float so it cannot find it. Try replacing these lines
intent.putExtra("consumOn", 4);
intent.putExtra("consumOff", 5);
with
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
because you haven't actually defined their type anywhere and they aren't variables.
You should pass bundle with your floats to intent and only then extract them
I am actually surprised that your intent is launching at all.. it seems like you only initialize the intent in the first case try moving Intent declaration out of the switch
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.menu_about:
intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
return true;
case R.id.menu_home:
return true;
case R.id.menu_settings:
intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
case R.id.menu_battstat:
intent = new Intent(MainActivity.this, StatsActivity.class);
intent.putExtra("consumOn", 4f);
intent.putExtra("consumOff", 5f);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Android action bar navigation

I've got spinner navigation on my action bar and have a navigation listener for this.
When the activity is created the listener (below) picks up on the default spinner item which means case 0 is run on creation, opening another activity.
How do I stop it registering a navigation change when the activity is created?
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
Intent i = new Intent(Main.this, Example.class);
startActivity(i);
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
Update:
Think I solved it with this, I declared a boolean flag, changed it to false oncreate.
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch(itemPosition) {
case 0:
if(flag == true) {
if(Main.class == Main.class) {
} else {
Intent i = new Intent(Main.this, Main.class);
startActivity(i);
}
}
if(flag == false) {
flag = true;
}
break;
case 1:
Intent i2 = new Intent(Main.this, Example.class);
startActivity(i2);
break;
case 2:
Intent i3 = new Intent(Main.this, Example.class);
startActivity(i3);
break;
case 3:
Intent i4 = new Intent(Main.this, Example.class);
startActivity(i4);
break;
}
return false;
}
};
There may be a simpler way but you could put a boolean flag in onCreate() to false and check for that in your listener then set it to true after the first time when it sets up so it won't run the Intent code when you first run it. Depending on what you need, you may want to put it in onResume() so it doesn't run if you back into this Activity

Categories

Resources