I'm trying to make a dialogue box with a positive bottom that changes the activity from SignupAcitvity to MainActivity. So I used the intent method to do this. However, once I tried this, I got the following error message:Cannot Resolve constructor 'Intent(com.androidcodefinder.loginscreendemo.Profile.ExampleDialogue, java.lang.Class<com.androidcodefinder.loginscreendemo.MainActivity>)'. Can you help me fix this?
My code:
public class ExampleDialogue extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialayout,null);
builder.setView(view)
.setTitle("Confirm Your Email")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(SignUpActivity.class, MainActivity.class);
startActivity(intent);
}
});
return builder.create();
}
}
The reason that you are getting this error is because you are in the ExampleDialogue activity, but you are starting an intent from SignUpActivity.class to MainActivity.class. If you want to go to MainActivity.class, you will have to do:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Also, it can't resolve the constructor so just add this default constructor, which you can change as you wish:
public ExampleDialogue(){
//Constructor code.
}
You are trying
Intent intent = new Intent(SignUpActivity.class, MainActivity.class)
However, Intent has no constructor with 2 classes as arguments. Hence the error.
What you want, is to use the constructor Intent(Context, Class<?>). Specifially, in your case you do this:
Intent intent = new Intent(getActivity(), MainActivity.class);
Related
I have an activity, let's call it MainActivity which has an SomeAdapter.
In the adapter's code I have
#Override
public void onBindViewHolder(#NonNull OptionViewHolder holder, final int position) {
final Option o = values.get(position);
holder.textView.setText(o.getOption());
holder.foreGround.setBackgroundColor(o.getOptionLayout().getBackGroundColor());
holder.editOptionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
}
});
}
When actually clicking the editOptionButton, I get the following stack trace
Process: com.company.app, PID: 20916
android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4544)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at com.company.app.adapters.OptionsAdapter$1.onClick(OptionsAdapter.java:86)
the context that calls startActivityForResult is the MainActivity and is not null and alive (visible) at the time of calling because the adapter runs in it.
Therefor, I have no idea why this error pops up nor what I could do about it. Do any of you may know why or have experienced it before?
You have to include the Activity name like this
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
First
you are initlizing the intent in wrong way, you need init it as below:
Intent intent = new Intent(context, SecondActivity.class);
Second
If the result do not return to your MainActivity then you need to cast the context to activity before starting the second activity as:
((Activity) context).startActivityForResult(intent,1);
This means that intent doesn't know where to go. So give the context and the activity name where u want to go.
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra("Key",value);
startActvity(intent);
I try to send some values to another activity.
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
Intent intent = new Intent(GroupsMain.this, AboutGroup.class);
intent.putExtra("groupName", "Hello");
startActivity(intent);
}
#Override
public void onLongClick(View view, int position) {
}
}));
And so on AboutGroup activity I try to get extra.
1 way:
Bundle extras = getIntent().getExtras();
String name = extras.getString("groupName");
and second way:
Intent intent = new Intent();
String name = intent.getStringExtra("groupName");
But nothing works for me. On AboutGroup activity i get empty string. Please help me fix this problem.
try this
Send:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("groupName", "Hello Anna");
startActivity(intent);
get extra:
String name = getIntent().getStringExtra("groupName");
myTextview.setText(name);
This question already has answers here:
How to pass a value from one Activity to another in Android?
(7 answers)
Passing ArrayList through Intent
(6 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I am developing a QR Code app for android. I want to pass whatever I scan into a new activity. This is my QR Code results code,
public void handleResult(final Result result)
{
final String scanResult = result.getText();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scanned Result");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i) {
scannerView.resumeCameraPreview(MainActivity.this);
}
});
builder.setNeutralButton ("Show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
startActivity(new Intent(MainActivity.this, ResultPage.class));
}
});
builder.setMessage(scanResult);
AlertDialog alert = builder.create();
alert.show();
}
ResultPage.class is the activity I wish to display the scanned result on to, how do I do this? Edit: My ResultPage.Class is currently empty with only an empty textview. I'd like the output to be in the textview.
final String scanResult = result.getText();
Intent intent = new Intent(getBaseContext(), ResultPage.class);
intent.putExtra("SCAN_RESULT", scanResult);
startActivity(intent);
Now you can find scanResult in ResultPage activity
String s = getIntent().getStringExtra("SCAN_RESULT");
If you have URI then pass that URI using Intent
if you want to pass image itself
Bitmap image= imageView.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
and in another activity receive like:
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
image.setImageBitmap(bmp );
You will have to pass the Resulted string from MainActivity to ResultPage.
For passing the data from one Activity to Another Activity, you can use Bundle.
Here is the code for sending data to another Activity:
builder.setNeutralButton ("Show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
Intent intent = new Intent(MainActivity.this,ResultPage.class);
Bundle bundle = new Bundle();
bundle.putString("RESULT",scanResult);
intent.putExtra(bundle);
startActivity(intent);
}
});
And now you can get this result to ResultPage Activity.
Here is the code for getting data.
Bundle bundle = getIntent().getExtras();
String scanResult = bundle.getString("RESULT");
Now you can Display your scanResult.
I have a notepad activity that opens up a window dialog version of it where user can go to another app and still type on the notepad
For both notepad to have the same content, I used broadcastReceiver to set the text of the notepad activity to the one in the dialog when the user is done.
Activity's BroadcastReceiver:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
et_editor.setText(globalVariable.getScriptEditorText());
Toast.makeText(Script_editor.this, "Script Editor have been updated", Toast.LENGTH_SHORT).show();
}
};
Close Button of the Dialog:
btn_Close = (Button) view.findViewById(R.id.button_Close);
btn_Close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(getApplicationContext())
.setTitle("Would you like to update the editor?")
.setNegativeButton("No, discard the changes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(ColorCoordinatePickerService.this, "Changes have been discarded", Toast.LENGTH_SHORT).show();
dialog.dismiss();
stopSelf();
}
})
.setPositiveButton("Yes, update with changes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
globalVariable.setScriptEditortext(CCEditor.getText().toString());
Intent intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);
dialog.dismiss();
stopSelf();
}
})
.create();
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
alertDialog.show();
}
});
It works BUT the user have to go back to the notepad activity (cannot be in another app or activity).
How do I update the notepad's content without having to "open" it up?
Queue the changes in BroadcastReceiver and when NotepadActivity resumes set the changes in the EditText. The queue doesn't need to be an actual queue, you can persist the last broadcast receiver data somewhere.
I am not sure, but it may be clear your global variable on moving from activity to another activity, also you are declaring the instance of global variable so it will set value in instance not in main variable and therefor you will not get it on receiver.
So I think you should set value in intent and then get it on receiver and manage it so it will work.
Check below, first your click event of dialog box method
#Override
public void onClick(DialogInterface dialog, int which) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
globalVariable.setScriptEditortext(CCEditor.getText().toString());
Intent intent = new Intent(BROADCAST_ACTION);
intent.putExtra("global_variable","" + CCEditor.getText().toString());
sendBroadcast(intent);
dialog.dismiss();
stopSelf();
}
And now get value in receiver as like below.
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final globalVariable globalVariable = (globalVariable) getApplicationContext();
String strData = intent.getExtras().getString("global_variable");
et_editor.setText(strData);
Toast.makeText(Script_editor.this, "Script Editor have been updated", Toast.LENGTH_SHORT).show();
}
};
Hope this will work
without opening acitivity,you want to update data then only one way you can do it.when you receive in receiver set all data into sharepreference.Whenever your notepad activity opens you can set take data from sharepreference and set it into notepad activity.
Use LocalBroadcastManager.
Example: How to use it
I have my MainActivity, and I create an Intent to a SecondActivity, when a button is pressed,
I want to return to activity. But when I create the intent and pass (this, MainActivity.class) through the constructor, I get an error: The intent constructor is undefined. Can someone help me?
public void goBackToMainActivity(View view) {
//Button for retrieving the user's current location:
final Button backButton = (Button) findViewById(R.id.back_button);
//Listens for button presses and releases:
backButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//When the button is pressed:
if(event.getAction() == MotionEvent.ACTION_DOWN) {
//Change image for touch indication:
backButton.setBackgroundResource(R.drawable.back_button);
//When the button is released:
} else if (event.getAction() == MotionEvent.ACTION_UP) {
//Change image back to default:
backButton.setBackgroundResource(R.drawable.back_button_selected);
goBackToMainActivity();
}
return false;
}
});
}
public void goBackToMainActivity() {
finish();
Intent startIntent = new Intent(this, MainActivity.class);
startActivity(startIntent);
}
I guess you are creating the intent inside an OnClickListener, thus you need to use the code below:
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
The reason is that if you do not specify SecondActivity.this, you are actually passing the OnClickListener into the intent constructor.
Change your goback method:
public void goBackToMainActivity() {
Intent startIntent = new Intent(CurrentActivityName.this, MainActivity.class);
startActivity(startIntent);
finish();
}
First clear all the existing activities by writing code:
startActivity(intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Then call the intent to the MainActivity
Intent i=new Intent(CurrentActivity.this, MainActivity.class);
startActivity(i);
Try calling finish() on button click in second activity to return to the main activity.
public void onClick(View v) {
finish();
}