Giter Club home page Giter Club logo

android_callactivity's Introduction

Android/Java Start another Activity

Example Application: Call Activity | Start Another Activity - Android

Start Activity

Simple start another activity

Structure for startActivity:

Intent i = new Intent(MainActivity.this, SecondActivity.class);

startActivity(i);

startActivity

Start Activity with Put Extra

Initialize another activity with parameters Using different types of parameters

Set PutExtras

Structure for startActivity with putExtras:

Intent i = new Intent(MainActivity.this, SecondActivity.class);

i.putExtra(getString("KEY", "StringValueExtra");

startActivity(i);

startActivityPutExtra

Example

Intent i = new Intent(MainActivity.this, SecondActivity.class);

i.putExtra(getString(R.string.EXTRA_STRING), "StringValueExtra");
i.putExtra(getString(R.string.EXTRA_INT), 1);
i.putExtra(getString(R.string.EXTRA_LONG), 1L);
i.putExtra(getString(R.string.EXTRA_BOOLEAN), true);
i.putExtra(getString(R.string.EXTRA_DRAWABLE), R.drawable.ic_android);
i.putExtra(getString(R.string.EXTRA_MODEL), new Person("NamePerson", "AnyPersonValue"));

startActivity(i);

Get PutExtras

In the second activity:

Intent i = getIntent();
Bundle extras = i.getExtras();

Check has extras (PutExtras)

if (extras == null) {
//TODO
}

Check contains key PutExtra

if (extras.containsKey("KEY"){
//TODO    
}

Example

if (extras != null) {
     if (extras.containsKey("KEY"){
         //TODO
     }
}

Start Activity for Result

Declare variable for request code.

int CODE_ANY = 100; // ANY VALUE;
Intent i = new Intent(MainActivity.this, SecondActivity.class);

startActivityForResult(i, CODE_ANY));

In the second activity

Intent i = new Intent();
i.putExtra("KEY", "THIS VALUE RETURN ACTIVITY FOR RESULT");
setResult(RESULT_OK);

finish();

โš ๏ธ Necessary Override method onActivityResult on first activity.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if( requestCode ==  CODE_ANY)) {

    if (resultCode == RESULT_OK) {
        if(data != null){
            if(data.getExtras().containsKey("KEY")){
            // TODO - has extra
            }
        }
    }  

}

startActivityForResult

Some links for more in depth learning

android_callactivity's People

Contributors

fefong avatar

Watchers

 avatar  avatar

Forkers

anuragpatil1704

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.