2

I've had no success discovering the technical terminology for what I am trying to describe, so here goes.

How can I, or why can't I open an intent as soon as I've landed on a new activity via an intent?

Example:

1) User is sent to a new activity after pressing a button (launches intent)

2) User arrives at new activity and now an event fires off automatically before the user has a chance to do anything else, and a new intent sends him to the next activity.

Also, in relation to fragments:

3) User is sent to a new fragment in the activity and now an event fires off automatically before the user has a chance to do anything else, and a new intent sends him to the next fragment.

Update: This doesn't seem to work for me when I put this in the onCreate method of one my fragments (1stFragment).

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("2ndFragment", true);
startActivity(intent);

It won't launch to the next fragment.

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153

1 Answers1

1

Just put a new intent in the onCreate method of the other activity.

1) User is sent to a new activity after pressing a button (launches intent)

Intent i = new Intent(OldActivity.this, NewActivityThatUserHasNoControlOf.class);
startActivity(i)

2) User arrives at new activity and now an event fires off automatically before the user has a chance to do anything else, and a new intent sends him to the next activity.

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.New_Activity_That_User_Has_No_Control_Of);

/////////////////////////////////////////////////////////////////////////

Intent i = new Intent(NewActivityThatUserHasNoControlOf.this, ThirdAndFinal.class);
    startActivity(i)

Thats it. The onCreate method is fired as soon as an activity starts, making it the best place to start the new intent.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • Interesting! What about launching to a new fragment within the same activity? Can I set an intent in the onCreate method of one of my fragments, or can you not move between fragments like that? – Martin Erlic Dec 20 '15 at 10:06
  • 1
    @bluemunch Yes, you can set an intent int the onCreate method of one of your fragments. If my answer was useful, dont forget to accept it! :) Take a look at this question: http://stackoverflow.com/questions/21028786/how-do-i-open-a-new-fragment-from-another-fragment – Ruchir Baronia Dec 20 '15 at 10:13
  • Thanks. Your answer has helped me. With respect to the question you linked, does this only work inside an onClickListener though? I need to fire this off in my fragment's onCreate. – Martin Erlic Dec 20 '15 at 10:23
  • @bluemunch No, the example of the question was of onClickListener, but it can be called anywhere. – Ruchir Baronia Dec 20 '15 at 10:28
  • Then what is the NextFragment class? I can't seem to import it. Is that just the name of the next fragment? – Martin Erlic Dec 20 '15 at 10:28
  • 1
    @bluemunch LOL. Yes, it is the name of the next fragment. :) – Ruchir Baronia Dec 20 '15 at 10:29
  • Sorry! I'm a newb. Thanks for all the help. – Martin Erlic Dec 20 '15 at 10:30
  • @bluemunch No problem! :) – Ruchir Baronia Dec 20 '15 at 10:30