자바 어플 개발/안드로이도 스튜디오 개념

Intent에 데이터를 input하는 방법

appmaster 2019. 7. 22. 13:19

Activity A에서 Activity B로 명시적으로 데이터를 받는 방법을 먼저 보여주겠다.

package fast.campust.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("LifeCycle","1 : onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(this,SecondActivity.class);
        intent.putExtra("INTENT_KEY",5); //intent에 데이터를 넣은 모습이다. 이때 반드시 startActivity전에 해야한다. 
        // 그이후에 넣으면 액티비티 작동한다음에 값을 입력한것이기 때문에 입력된값이 작동되지 않는다.
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        Log.d("LifeCycle","1 : onStart");
        super.onStart();
    }


    @Override
    protected void onResume() {
        Log.d("LifeCycle","1 : onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d("LifeCycle","1 : onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d("LifeCycle","1 : onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d("LifeCycle","1 : onDestroy");
        super.onDestroy();
    }


    @Override
    protected void onRestart() {
        Log.d("LifeCycle","1 : onRestart");
        super.onRestart();
    }
}

이렇게 해서 값을 보냈다. 그리고 다음 액티비티에서 이것의 값을 받을 것이다.

 

 

package fast.campust.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SecondActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("LifeCycle","2 : onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Intent intent = getIntent();
        String data = intent.getStringExtra("INTENT_KEY");
        Log.d("intent_key", data);
        
        //이것은 get을 이용해서 intent를 생성한 것이다.

    }
    @Override
    protected void onStart() {
        Log.d("LifeCycle","2 : onStart");
        super.onStart();
    }


    @Override
    protected void onResume() {
        Log.d("LifeCycle","2 : onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d("LifeCycle","2 : onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d("LifeCycle","2 : onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d("LifeCycle","2 : onDestroy");
        super.onDestroy();
    }


    @Override
    protected void onRestart() {
        Log.d("LifeCycle","2 : onRestart");
        super.onRestart();
    }
}

 

 

 

 

암시적 intent를 사용하는 방법은 이것이다.

package fast.campust.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d("LifeCycle","1 : onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* 명시적 intent
        Intent intent = new Intent(this,SecondActivity.class);
        intent.putExtra("INTENT_KEY",5);
        intent.putExtra("INTENT_KEY_STRING","STRING");
        startActivity(intent);
         명시적 intent */

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
        startActivity(intent);
    }

    @Override
    protected void onStart() {
        Log.d("LifeCycle","1 : onStart");
        super.onStart();
    }


    @Override
    protected void onResume() {
        Log.d("LifeCycle","1 : onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.d("LifeCycle","1 : onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.d("LifeCycle","1 : onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.d("LifeCycle","1 : onDestroy");
        super.onDestroy();
    }


    @Override
    protected void onRestart() {
        Log.d("LifeCycle","1 : onRestart");
        super.onRestart();
    }
}

 암시적 Intent를 사용 할때는 명시적 intent(앱을 화면으로 전환할때)와 달리 내 어플리케이션밖에 있는 다른 어플리케이션을 요청 할때 사용이된다. 시스템에게 요청할때도 이용이 된다.

 

이렇게 코드를 입력하게 되면 google인터넷 창이 잘 떠진것을 볼 수가 있다.

'자바 어플 개발 > 안드로이도 스튜디오 개념' 카테고리의 다른 글

Stack  (0) 2019.07.23
Intent  (0) 2019.07.22
Activity  (0) 2019.07.22