1

[Update Solution] Referring to the post in the link

ViewPager PagerAdapter not updating the View

public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
    Log.w(TAG,"Some button clicked !!");
    getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);

    mViewPager.getAdapter().notifyDataSetChanged();
}

 // And inside my PagerAdapter
    @Override
    public int getItemPosition(Object object) {
        return 0;
    }

Fixed all my problems, i just used Intent.setAction().

[Update to below Post]

My problem is i have a ViewPager PagerAdapter in my Main Activity. On clicking one of the 3 buttons., any specific intent will be fired (i used intent, i could have gone with just State variable as well, just that i pass some Uri with the intent). What happens is., i do

public void onSomeButtonClicked(View view) { // registered to Button's android:onClick in the layout xml file
    Log.w(TAG,"Some button clicked !!");
    getIntent().setAction(IntentManager.Intents.ACTION_SPAWN_LIST_BY_SOMETHING);

    mViewPager.getAdapter().notifyDataSetChanged();
}

This is why i was guessing maybe i should just do startActivity again, with the new Intent Action on the same Activity.

Spawning a new Activity, i would have to redraw every other view in the layout which is basically the same code, for the most part in the current activity.

Any suggestions here? Please help

[Original Post]

I have a PagerAdapter and 3 Buttons in the my Main Activity. This activity is enter from Main Launcher.

When i press any one of the buttons, the Intent Action is changed.

My question:

  1. The changed Intent action reflects some changed view in the ViewPager and does_not spawn a new Activity as such, only the view is updated.

What approach should i take to get this task?

Can i start the currentActivity using startActivity() and different Intent actions on button click?

or is there any other efficient way in android to do this?

[No need code, just explanation of logic / method would suffice]

Thanks in advance

Community
  • 1
  • 1
devgp
  • 1,321
  • 3
  • 17
  • 36
  • I'm a little confused at the question, if you're just trying to update some state of your application then you can just call a method or if you're trying to move to another Activity you use an Intent to start a new activity. – Dan S Nov 21 '11 at 23:50

2 Answers2

1

If you are saying that you are trying to use startActivity to bring up the same activity again, and its not working, it could be because you set something like singleTop in your Android manifest.

If you are asking whether or not you should use an intent to change the state of your Activity, then the answer is "it depends". If the user would expect the back button to return your app to its previous state (instead of going back to the home screen), then it might be a good choice for you. If that is the case, however, I would ask why not just make 2 different Activities? Otherwise, just do as Dan S suggested and update the state of your Activity as the user interacts with it.

Justin Breitfeller
  • 13,737
  • 4
  • 39
  • 47
1

You can always use the onNewIntent() hook. Do something like this in your activity:

protected void onNewIntent(Intent intent){
  //change your activity based on the new intent
}

Make sure to set the activity to singleTop in your manifest. Now whenever startActivity is called the onNewIntent() will be executed. Also, note that per the documentation:

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Kurtis Nusbaum
  • 30,445
  • 13
  • 78
  • 102