Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to pass value from one Activity to Another through Bundle Intent?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 300
    Comment on it

    1)Suppose need to pass value from MainActivity to SubActivity.We will first make xml of both Activity:

    activity_main.xml
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <EditText
            android:id="@+id/etFirst"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter Name"/>
        <Button
            android:id="@+id/btnClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etFirst"
            android:text="@string/click"
            android:layout_marginTop="20dp"/>
    </RelativeLayout>
    
    
    
     activity_sub.xml
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="value.com.valuebundle.SubActivity">
        <TextView
            android:id="@+id/tvShow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    


    2)Now in MainActivity,onCreate will be:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etFirst=(EditText)findViewById(R.id.etFirst);
        btnClick=(Button)findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle=new Bundle();
                bundle.putString("first",etFirst.getText().toString());
                Intent intent=new Intent(MainActivity.this,SubActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    

    Here we are asking user to enter value in EditText and on a Button click,we are passing that value from MainActivity to SubActivity through Bundle and putting that Bundle in Intent.


    3)Now in SubActivity,onCreate will be:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        tvShow=(TextView)findViewById(R.id.tvShow);
        Bundle bundle=getIntent().getExtras();
        String first=bundle.getString("first");
        tvShow.setText("welcome"+" "+first);
    }
    

    Here we have received the Bundle by Bundle bundle=getIntent().getExtras() and set it in TextView.

    That's it.Hope it helps:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: