Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:27:24+00:00 2026-05-28T07:27:24+00:00

Brand new to android and facing a weird problem.Check out the code below FirstActivity.java:

  • 0

Brand new to android and facing a weird problem.Check out the code below

FirstActivity.java:

package experiment.on.it;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class FirstActivity extends Activity implements Button.OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button timeDatePicker = (Button) findViewById(R.id.timeDatePickerBtn);
        Button customSpinner = (Button) findViewById(R.id.spinnerBtn);

        timeDatePicker.setOnClickListener(this);
        customSpinner.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

        case R.id.timeDatePickerBtn:
            startActivity(new Intent(this, TimeDatePicker.class));
        case R.id.spinnerBtn:
            startActivity(new Intent(this, CustomSpinner.class));
        }

    }
}

As I/you expected that by clicking on the two buttons i.e.

timeDatePicker

customSpinner

a new Activity will starts (TimeDatePicker and CustomSpinner respectively).

Our expectation is correct. But the problem i’m facing that when i click on the timeDatePicker the second activity (CustomSpinner) starts. But if i press the emulator’s back button (i.e. the device back button) the first activity(TimeDatePicker) appears. I cant find any keyword to describe this kind of problem. So writing this boring question. Any help will be greatly appreciated from a beginner.(…And i think the answer will be pretty easy and small, which i couldnt get. :-P) The Activities code are given below.

TimeDatePicker.java:

package experiment.on.it;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

public class TimeDatePicker extends Activity {

    private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;
    private TextView mTimeDisplay;
    private Button mPickTime;

    private int mhour;
    private int mminute;

    static final int TIME_DIALOG_ID = 3;

    static final int DATE_DIALOG_ID = 2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timedatepicker);

        mDateDisplay = (TextView) findViewById(R.id.date);
        mPickDate = (Button) findViewById(R.id.datepicker);
        mTimeDisplay = (TextView) findViewById(R.id.time);
        mPickTime = (Button) findViewById(R.id.timepicker);

        // Pick time's click event listener
        mPickTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(TIME_DIALOG_ID);
            }

        });

        // PickDate's click event listener
        mPickDate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DATE_DIALOG_ID);

            }
        });

        final Calendar c = Calendar.getInstance();
        mYear = c.get(Calendar.YEAR);
        mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mhour = c.get(Calendar.HOUR_OF_DAY);
        mminute = c.get(Calendar.MINUTE);

    }

    // -------------------------------------------update
    // date----------------------------------------//
    private void updateDate() {

        mDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/").append(mMonth + 1).append("/")
                .append(mYear).append(" "));
        showDialog(TIME_DIALOG_ID);

    }

    // -------------------------------------------update
    // time----------------------------------------//
    public void updatetime() {
        mTimeDisplay.setText(new StringBuilder().append(pad(mhour)).append(":")
                .append(pad(mminute)));
    }

    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }

    // Datepicker dialog generation

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            mYear = year;
            mMonth = monthOfYear;
            mDay = dayOfMonth;
            updateDate();
        }
    };

    // Timepicker dialog generation
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mhour = hourOfDay;
            mminute = minute;
            updatetime();
        }

    };

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                    mDay);

        case TIME_DIALOG_ID:
            return new TimePickerDialog(this, mTimeSetListener, mhour, mminute,
                    true);

        }
        return null;
    }
}

CustomSpinner.java:

package experiment.on.it;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class CustomSpinner extends Activity {

    String[] items = { "this", "is", "a", "really", "really2", "really3",
            "really4", "really5", "silly", "list" };

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.spinnerlayout);
        
        Spinner spin =(Spinner)findViewById(R.id.sPinner);
        ArrayAdapter adapter = new ArrayAdapter(this,R.layout.radio,R.id.txt,items);
        spin.setAdapter(adapter);
        
        
    }

    
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:27:25+00:00Added an answer on May 28, 2026 at 7:27 am

    You are missing break; statement in your switch case block.

    So, thought the first activity starts and is in your backstack, it is immediately pushed down by the second one due to the missing break and the back button behaves as it should and return you to the the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm brand new to JAva/Eclipse/Android but gearing up! I've got everything installed and working
I am brand new to android development and the code crashes at the setContentView(R.layout.main).
Brand new to Cocoa and I'm trying to figure out how to copy an
I'm brand new to Android development and right now I am building a simple
I am brand new to developing for android and have hit something of a
I am a brand new Java developer (I have been working in asp.net) and
I'm brand new to Android dev and have some n00b questions about AVD. Is
The brand-new WCF-based code needs (in the meanwhile) to provide a service to a
I'm brand new with log4j and I can't seem to figure this out. If
I'm brand new to Android development... coming from iPhone and .Net background. I've seen

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.