Android Open Source - 2014-Droid-code Activity1






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cs518.sample.multiactivity;
//from  www .  j a  v  a 2  s. com
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.net.Uri;
import android.util.Log;

/*
 * icon: elvis, holmes, monroe from http://iconka.com/persons/
 * icon: targetarrow from https://www.iconfinder.com/iconsets/5-o-clock-shades-icon-set-2#readme
 */
public class Activity1 extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity1);  
  }

  // use explicit intent -> name the target Activity class
  // send no data, receive no data back
  public void handleButton1Click(View view) {
    startActivity(new Intent(this, Activity2.class));
  }


  // use implicit intent -> (action, data.for.action)
  /*
   * Implicit intent is send to the Android system, it searches for all
   * components which are registered for the specific action and the fitting
   * data type. (Registered means AndroidManifest -> <intent-filter>
   */
  public void handleButton2Click(View view) {
    String url = "http://www.xkcd.org/";
    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

    /*
     * can also add the url using Intent.setData()
     * i.setData(Uri.parse(url)); can also add the action using
     * Intent.setAction() i.setData(Intent.ACTION_VIEW);
     */
    startActivity(i);
  }
  // use explicit intent -> name the target Activity class
  // send no data, expect a result back
  public void handleButton31Click(View v) {
    Intent i;
    i = new Intent(this, Activity3.class);
    /*
     * can also add the class using Intent.setClass() i.setClass(this,
     * Activity3.class)
     */
    /* int, requestCode = Constants.REQUEST1
     * requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.
     * */
    startActivityForResult(i, Constants.REQUEST1);

  }

  // use explicit intent -> name the target Activity class
  // send  data, expect a result back
  public void handleButton32Click(View v) {
    Intent i;
    i = new Intent(this, Activity4.class);
    /*
     * can also add the class using Intent.setClass() i.setClass(this,
     * Activity3.class)
     */
    i.putExtra(Constants.KEY3, "here's some data from main activity");
    startActivityForResult(i, Constants.REQUEST2);

  }
  public void handleButton4Click(View view) {
    Intent i = new Intent(this, Activity5.class);
    EditText et = (EditText) findViewById(R.id.dataforactivity);
    logIt(et.getText().toString());
    i.putExtra(Constants.KEY1, et.getText().toString());
    i.putExtra(Constants.KEY2, "second piece of data for activity");
    startActivity(i);
  }

  public void handleButton5Click(View view) {
    Intent i = new Intent(this, Activity6.class);
    startActivityForResult(i, Constants.REQUEST3);
  }
  /*
   * 
   * Called when result returned by an activity
   * 
   * (non-Javadoc)
   * 
   * @see android.app.Activity#onActivityResult(int, int,
   * android.content.Intent)
   * 
   * result:  RESULT_OK and RESULT_CANCELED in Activity class, set by returning
   * activity
   * request:  same one sent in startActivityForResult(Intent, request);
   */
  protected void onActivityResult(int request, int result, Intent data) {
    int counter;
    String msg = "activity " + result + " returned from request: "
        + request;
    TextView tvresult = (TextView) findViewById(R.id.result);
    TextView tv = (TextView) findViewById(R.id.data);
    switch (request) {
    // 1 sent to Activity 3 so result is from Activity 3
    case Constants.REQUEST1:      

      if (result == RESULT_OK) {
        logIt(msg += " result ok");
        toastIt(msg + " result ok");
      }
      if (result == RESULT_CANCELED) {
        logIt(msg += " result canceled");
        toastIt(msg + " result canceled");
      }
      tvresult.setText(msg);
      tv.setText("");
      break;
    // 2 sent to Activity 4 so result is from Activity 4
    case Constants.REQUEST2:       

      if (data != null && data.hasExtra(Constants.KEY4)) {
        counter = data.getExtras().getInt(Constants.KEY4);
      } else {
        counter = 0;
      }
      if (result == RESULT_OK) {
        msg = msg+" key4 data count: "+Integer.toString(counter)+" result ok";
        logIt(msg);
        toastIt(msg);
      }
      if (result == RESULT_CANCELED) {
        msg = msg + Integer.toString(counter)+" result canceled";
        logIt(msg);
        toastIt(msg);
      }
      tvresult.setText(msg);
      tv.setText("");
      break;
    // 3 sent to Activity 6 so  result is from Activity 6
    case Constants.REQUEST3:
      tvresult.setText(msg);
      if (result == RESULT_OK) {
        
        if (data.hasExtra(Constants.RETURN_KEY1)) {
          msg = "key1 data: " + data.getExtras().getString(Constants.RETURN_KEY1);
          logIt(msg);
        }
        if (data.hasExtra(Constants.RETURN_KEY2)) {
          msg += " key2 data: " + data.getExtras().getDouble(Constants.RETURN_KEY2);
          logIt(msg);
        }  
        tv.setText(msg);
      } else {
        logIt("Error, invalid return from Activity");
      }
      
      break; 
    default:
      logIt("no valid request received " + msg);
      toastIt("no valid request received " + msg);
      tvresult.setText(msg);
    }
  }// onActivityResult


  public static void logIt(String msg) {
    final String TAG = "MULTACT";
    Log.d(TAG, msg);
  }

  public void toastIt(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
  }
}




Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java