Search code examples
androidandroid-intentbundler

send int value from activity one to activity two


Hi I'm trying to send int value from activity one to activity two using this code

@Override
        public void onClick(View v) {
            new ChromaDialog.Builder()
                    .initialColor(getResources().getColor(R.color.colorAccent))
                    .colorMode(ColorMode.ARGB)
                    .indicatorMode(IndicatorMode.HEX)
                    .onColorSelected(new OnColorSelectedListener() {
                        @Override
                        public void onColorSelected(@ColorInt int color) {
                            Intent intent = new Intent(MainActivity.this, Hackpage.class);
                            intent.putExtra("intVariableName", color);
                            Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
                        }
                    })
                    .create()
                    .show(getSupportFragmentManager(), "ChromaDialog");


        }
    });
}

but I have a problem in value i get 0

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_hackpage);
    Texthack = (TextView)findViewById(R.id.hacktext);
    Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);
    Texthack.setBackgroundColor(Color.parseColor("#"+Integer.toHexString(intValue )));

Solution

  • //global:
    static final String SOME_ACTION = "com.example.test.myapplicationtest.SOME_ACTION";
    IntentFilter intentFilter = new IntentFilter(SOME_ACTION); //youre project package name. example "com.example.yourprojectpackagename.SOME_ACTION".
    
    
    
           @Override
        public void onClick(View v) {
            new ChromaDialog.Builder()
                    .initialColor(getResources().getColor(R.color.colorAccent))
                    .colorMode(ColorMode.ARGB)
                    .indicatorMode(IndicatorMode.HEX)
                    .onColorSelected(new OnColorSelectedListener() {
                        @Override
                        public void onColorSelected(@ColorInt int color) {
                registerReceiver(mReceiver, intentFilter);
                Intent intent = new Intent(SOME_ACTION);
                intent.putExtra("intVariableName", color);
                sendBroadcast(intent);
                Toast.makeText(MainActivity.this,"color :"+Integer.toHexString(color),Toast.LENGTH_LONG).show();
                        }
                    })
                    .create()
                    .show(getSupportFragmentManager(), "ChromaDialog");
        }
    });
    

    }

         ///
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
           int value =  intent.getIntExtra("intVariableName",0);
            Toast.makeText(getApplicationContext(),""+value,Toast.LENGTH_LONG).show();
        }
    };
    

    it's works well. Sending value to existing Activiy. (example. MainActivity.java). So you do not need to use Hackpage.class.