Joey Roosing
Joey Roosing

Reputation: 2155

Hopping between the same activity with next and previous button

I have searched and not found any clear answers to a problem im having.

In my application, I have an activity where you answer to a checkpoint, send it and go to the next one. A user should also be able to skip, and jump back to the previous checkpoint.

Let me support my story with a bit of code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.checkpointform);
    go = (GlobalObject)getApplication();

    _checkPoints = new ArrayList<CheckPoint>();
    _checkPoints.addAll(go.getCheckPoints());

    _currentActiveItemId = getIntent().getIntExtra("checkPointId", 0);
    int count = 0;
    while(count < _checkPoints.size()) {
        if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
            _checkPointTitle = _checkPoints.get(count).getCheckPointTitle();
        }
        count++;
    }

    final TextView tvCheckPointTitle = (TextView)findViewById(R.id.txtCheckpoint);
    tvCheckPointTitle.setText(_checkPointTitle);

    this.registerButtonEvents();

    _simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");

    Log.d(TAG, "On create");
}

This code is not optimized. I want it to work first before I do that so don't be hard on me for that, but tips are always very welcome.

So what happens here. A global(applicationwide) object is instantiated. I get an arraylist and store it in a local arraylist.

I take the extra from the screen that got me here. I loop trough the list to compare the currentselected item id with primary key, to get the right data to show.

I then register button events like so:

private void registerButtonEvents() {
    ImageButton btnTakePicture = (ImageButton)findViewById(R.id.btnTakePicture);
    btnTakePicture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            takePictureButton();            
        }         
    });

    Button btnPreviousCheckPoint = (Button)findViewById(R.id.btnPrev);
    btnPreviousCheckPoint.setOnClickListener(new View.OnClickListener() {   
        @Override
        public void onClick(View v) {
            previousCheckPoint();
        }
    });

    Button btnNextCheckPoint = (Button)findViewById(R.id.btnNext);
    btnNextCheckPoint .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            nextCheckPoint();
        }
    });
}

The methods called in the onclick events don't do anything yet. This is where I need help.

What I need is, the current activity to be destroyed, but before that happens, call this activity.. could be something like: Intent intent = new Intent(this, checkPointFormActivity);

And give the next/previous activity the currentselected id + 1 (if the next item is null, ill get the first item in the list.. this is logic ill worry about later)

However I have been unsuccesfull in calling this activity.

So in short:

I need to call the same activity as the current one, with new data. Upon doing that, I want to destroy/finish/delete/un-do the previous activity. (lets say checkpoint 1 is the current activity. I press next, this activity should be refreshed with new data/finished and be recalled with new data passed in the intent in the form of an ID.

I hope I am providing enough information this way. If anything is unclear. And it would not surprise me. Please let me know so I can edit.

What I tried is

private void nextCheckPoint() {
    int count = 0;
    int listLength = _checkPoints.size();

    while(count < listLength) {
        if(_currentActiveItemId == _checkPoints.get(count).getPrimaryKey()) {
            final Intent intent = new Intent(this, CheckPointFormActivity.class);
            intent.putExtra("checkPointId", _currentActiveItemId + 1);
            startActivity(intent);
        }
    }
}

and also ttried calling finish() after and before startactivity. It results in my app not responding at all.

Thanks alot for even taking the time to read the question. Hope to see an answer or any advice of how to imrpove the flow soon. Thanks.

Upvotes: 0

Views: 1168

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

I may not have understood you correctly, but what's the reason for re-instantiating the activity to navigate? Why don't you:

  1. Just refresh your widgets with the information for the new checkpoint when the user navigates?
  2. Or use a ViewFlipper to navigate between the views within the same activity?
  3. Or use the ViewPager to use fragments the user can navigate using gestures?

EDIT
You say you do not know how to refresh the widget with new information. This should be fairly easy.

You have event handlers for the navigation buttons, so you can determine the new checkpoint, the data of which you want to display. Each control in the view that displays checkpoint-specific data must be changed for the new checkpoint.

For example, the name of the checkpoint could be displayed within a TextView. Then you'd have to update the TextView like:

TextView checkPointTitle = (TextView)findViewById(R.id.checkPointTitle);
checkPointTitle.setText(newCheckPointTitle);

You could create a method that takes the ID of the checkpoint (may be the index is a list of objects for the checkpoints or a database ID or whatever) and updates the controls. Then you call this method from your previousCheckPoint and nextCheckPoint methods after determining the ID of the checkpoint to be displayed. Of course, you call this method also when the Activity is displayed for the first time for the first checkpoint.

Upvotes: 2

Related Questions