1

I've been trying to get a sequence of classes to open:

Page01.java, Page02.java, Page03.java, Page04.java, etc. for 40 pages

Is there any way get my next button to automatically open the next class without me having to change the class name every time inside the intent?

Something like this:

public void Next(View v){
    Intent next = new Intent(this, "Get the next class on increment of 1");
    startActivity(next);
    finish();

Thanks!!

Violin Nz
  • 297
  • 1
  • 5
  • 15
  • 1
    Are you saying you have 40 different Activities each with a totally different layout??? – Squonk Nov 03 '13 at 18:15
  • That's right!, but I sense that's not the way to do it, right? – Violin Nz Nov 03 '13 at 18:20
  • Hmmm, it certainly doesn't sound right but without knowing what your app does it's hard to say. Also a linear progression through Activities isn't normally the way an Android app is built. Are you sure you can't just change the content view or use Fragments perhaps hiding or making visible different UI elements? – Squonk Nov 03 '13 at 18:28
  • @ViolinNz: Well, it's propably not [DRY](http://en.wikipedia.org/wiki/Don%27t_repeat_yourself). You should only do that, if they are totally different, i.e. different layout, different logic and whatever, and you absolutely can't use one Activity. Because, even when you put most of the work in one Superclass, you still got 40?!? classes, 40 entries in the manifest. .. If you can't combine them all in one Superclass, how about 3 or 4? – tilpner Nov 03 '13 at 19:00

2 Answers2

2

Don't. Do. That.


Create a class SinglePageActivity which takes an extra parameter page_id:

public class SinglePageActivity extends Activity {
    private int pageId;

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        pageId = getIntent().getIntExtra("page_id", 1);

        // You can now decide what to display depending on pageId
        switch(pageId) {
        case 1:
            // Code for page 1
            setContentView(R.layout.page_1);
            break;
        case 2:
            // Code for page 2
            setContentView(R.layout.page_2);
            break;
        ...
        }
    }

    public void Next(View v) {
        Intent next = new Intent(this, SinglePageActivity.class);
        next.putExtra("page_id", pageId + 1);
        startActivity(next);
    }

}

You don't have to use different layouts, but you can.


I know, this does not (exactly) answer your question. But in my opinion this is the only viable way of handling those 40 pages.

And, as we are already talking about application design, it is quite unusual to use different activites for parallel (sibling) contents. You might want to replace the content of one activity once the button is pressed.

Tobias
  • 7,723
  • 1
  • 27
  • 44
  • If I use that, how can I use different layouts? ("setContentView(R.layout.xx);?) – Violin Nz Nov 03 '13 at 18:27
  • You could also directly pass the layout resId int, so you don't have to switch on it... That makes reordering and so on easier, and moves the code out of SinglePageActivity. – tilpner Nov 03 '13 at 18:56
  • @StackOverflowException Yes, but I did not expect the layout to be the only difference. Anyway, you are right. – Tobias Nov 03 '13 at 19:18
  • That sounds logical and great. Now, how can implement "directly pass the layout resId int, so you don't have to switch on it". I can' really grasp the concept. Thank you so far, great info. – Violin Nz Nov 07 '13 at 03:16
  • "You might want to replace the content of one activity once the button is pressed." How do I do that? – Violin Nz Nov 28 '13 at 00:56
  • @ViolinNz I personally like to use [`ViewPager`s](https://developer.android.com/reference/android/support/v4/view/ViewPager.html) for sibling pages but you could also just [replace the content](http://stackoverflow.com/a/6812176/2948765). – Tobias Nov 28 '13 at 07:58
0

Try like this,

Make Sure that your Activity class should be in the Same package.

 final String packageName = this.getClass().getPackage().getName();
 final Context context = this;
 String[] arrayOfActivity[]=new String[]{"Page01","Page02","Page03"......,"Page40"}

                      try {
                            Class c = Class.forName(packageName + "." + arrayOfActivity[position]);
                            startActivity(new Intent(context, c));
                        } catch (ClassNotFoundException e) {
                            Toast.makeText(context, String.valueOf(e), 5000).show();
                        }
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33