Object vs Android Activity

I'm trying to make a time-sheet app for myself that I can use at work. I've pasted the code for my two classes below. My conundrum is not having to pass or keep data just how to go back to the original view if I've opened a canvas object. Is it possible to use an intent to open an activity rather than just creating the "tv" object (in openTEV method)? That would mean that when I tap the "back" button on the bottom right of my Galaxy phone it will stop that activity and go back to the first one with the EditText objects that I've entered strings into. At the moment hitting the back button ends the entire app. Or am I misunderstanding how android works?

MainActivity.java -

public class MainActivity extends Activity {

private MyTouchEventView tv;
private EditText et1;
private EditText et2;
private EditText et3;
private EditText et4;

public void onCreate(Bundle savedInstanceState) {
    et1 = (EditText) findViewById(R.id.editText1);
    et2 = (EditText) findViewById(R.id.editText2);
    et3 = (EditText) findViewById(R.id.editText3);
    et4 = (EditText) findViewById(R.id.editText4);

public void processForm(View v) {
    String txt = et1.getText().toString();
    Toast.makeText(this.getApplicationContext(), txt, Toast.LENGTH_LONG).show();

protected void onPause() {
    Log.i("TAG", "Paused");

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {
        case R.id.action_save:
            return true;
        case R.id.action_sign:
            return true;
            return super.onOptionsItemSelected(item);

public void saveCanvasImage() {

    Bitmap bm = tv.getDrawingCache();

    String fPath = Environment.getExternalStorageDirectory().toString();
    fPath = fPath + "/Pictures";
    File f = null;
    String s = getCurrDate();
    s = s + ".png";
    f = new File(fPath, s);

    try {
    FileOutputStream strm = new FileOutputStream(f);
    bm.compress(CompressFormat.PNG, 80, strm);
    catch (IOException e){


public void openTEV() {

    tv = new MyTouchEventView(this);
    addContentView(tv.btnReset, tv.params);


public String getCurrDate()
    String dt;
    Date dNow = new Date();
    SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMddkkmmss");
    dt = sdf.format(dNow);

    return dt;

MyTouchEventView.java -

public class MyTouchEventView extends View {

private Paint paint = new Paint();
private Path path = new Path();

public Button btnReset;
public LayoutParams params;

public MyTouchEventView(Context context) {


    btnReset = new Button(context);
    btnReset.setText("Clear Screen");

    params = new LayoutParams(LayoutParams.MATCH_PARENT,

    btnReset.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            // resets the screen

            // Calls the onDraw() method



protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);

public boolean onTouchEvent(MotionEvent event) {

    // Gives you x and y coordinates on the Event.
    float pointX = event.getX();
    float pointY = event.getY();

    // Checks for the event that occurs
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        path.moveTo(pointX, pointY);
        return true;
    case MotionEvent.ACTION_MOVE:
        path.lineTo(pointX, pointY);
    case MotionEvent.ACTION_UP:
        return false;

    // Schedules a repaint.
    // Force a view to draw.
    return true;


As you said, you want to open a new activity from your openTEV method...am I right??? to achieve this, first of all you have the activity that you want to start from activity.

Suppose, you have two activities which are MainActivity and BActivity. Now, you want, when openTEV metod will be executed then BActivity should start from MainActivity. Now what you have to do is to write the following code into your openTEV method...

Intent intent = new Intent(MainActivity.this, BActivity.class);

Now when you will press back button in BActivity then you will return MainActivity.

You haven't "opened" your "canvas object". Your canvas object is a View, like any other view e.g. TextView, ImageView, EditText etc. You have extended the View class to make a new type of view.

Here, you set the contents of your one and only activity to your custom view.

 tv = new MyTouchEventView(this);

As you only have one activity, when you press the back button, your app will end - since there are no more activities to display to the user.

To be able to interact with MyTouchEventView, create a new activity, set it's content to your view as you do above, then launch the new activity with an Intent. When you press the back button, you will return to your current activity, MainActivity.

It is not usual to use setContentView more than once in an activity. Think of an activity as a single, focused thing which the user interacts with - a screen if you like.


I see that you are creating a button in your view. This is not correct. a View is the lowest level of interface component. A View cannot contain other views. To do that, you use a ViewGroup such as LinearLayout, RelativeLayout etc. It is also possible to add your custom view to your XML layout so that you do not have to create it in code. It is normally good practice to separate code and UI.

See this for how to add your view to a layout XML


Think if i am not wrong, than just make a child activity. While creating a new activity you will get the options for showing one of the activities u already have- as the new activities parent activity. So, in this way you will get a back navigation inaction bar, by which u can easily navigate to ur parent activity from child. ANd on the mainActivity(parent) just use intent to go to ur child activity.

Intent intent = new Intent(MainActivity.this, BActivity.class); //BActivity-the child activity

Need Your Help

Android layout/control to show "arrow" from opened Tab to List/Menu?

android android-layout

I like the design of the Gmail app for android and I'm wondering if they created it using some standard approach.

node.js +socket.io is not working on heroku

javascript node.js heroku websocket sensor

Following part of my code is used for retrieving the data from TI sensor tag. So we are using sensortag node.js module to get the data and sending it to client using socket.io. on local host the