There must be a way to get intent result in a class without using onActivityResult. By using other methods...
I dont know how, but Iam sure there is a way.
My class that should get the result of the intent filepicker from this class without using onActivityResult in the MainActivity.java that extents activity. FilePicker.java
package com.hadiawali.codeeditor;
import android.content.Intent;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
public class FilePicker {
Intent filePicker = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
public static void startPicking(Activity activity) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent chooseFolder = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
chooseFolder.addCategory(Intent.CATEGORY_DEFAULT);
activity.startActivityForResult(Intent.createChooser(chooseFolder, "Choose directory"), 9999);
}
}
}
My class that extents activity. MainActivity.java
package com.hadiawali.codeeditor;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = findViewById(R.id.btn);
btn.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
FilePicker.startPicking(MainActivity.this);
//I need to get the intent reslut from the class without using onActivityResult
}
});
}
}
For example you send to intent from activity A to B then
in Activity A create Activity Result like below
ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
// callback called
}
}
});
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//call B activity
someActivityResultLauncher.launch(intent);
}
in Activity B your task is successfully complete then set
setResult(RESULT_OK);
finish();
or if task is not complete successfully then put
setResult(RESULT_CANCELED);
finish();
Related
I am new to Android Studio and I am creating a custom notification app, and I wanted to use the EditText from my MainActivity class in Broadcast Receiver class. How can I do that?
Broadcast Receiver code:
`package com.example.notificationscreator;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.EditText;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder Build = new NotificationCompat.Builder(context, "Notified");
Build.setSmallIcon(R.drawable.ic_stat_name);
Build.setContentTitle("");
Build.setStyle(new NotificationCompat.BigTextStyle().bigText(""));
NotificationManagerCompat Managercompats = NotificationManagerCompat.from(context);
Managercompats.notify(1, Build.build());
}
}`
Main activity code:
`package com.example.notificationscreator;
import static com.example.notificationscreator.R.*;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.AlarmManager;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Calendar;
import java.util.Random;
public class MainActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main2);
Button btnmain = findViewById(R.id.button4);
Button Displaynotif = findViewById(R.id.button3);
EditText Timedisplay = findViewById(R.id.editTextTime);
Integer Time = Integer.parseInt(Timedisplay.getText().toString());
btnmain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Backmainpage();
}
});
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("Notified","Notification", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
Displaynotif.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int Randnum = new Random().nextInt(80);
Intent intent= new Intent(MainActivity2.this,MyReceiver.class);
PendingIntent pendingintention = PendingIntent.getBroadcast(MainActivity2.this,0, intent,0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
long timeonclick = System.currentTimeMillis();
long timeafterclick = 10000;
am.set(AlarmManager.RTC_WAKEUP,timeonclick+timeafterclick,pendingintention);
}
});
}
public void Backmainpage(){
Intent intention2 = new Intent(this,MainActivity.class);
startActivity(intention2);
}
}`
I've tried recalling Main Activity using
MainActivity2 Mainactivity = new MainActivity2();
but I still can't access the UI from Main Activity
You can use another BroadcastReceiver to communicate with your activity like below example.
add the following code inside your broadcast receiver.
#Override
public void onReceive(Context context, Intent intent) {
//... your other code
Intent intent = new Intent();
intent.setAction("CustomAction"); // use your action name
intent.putExtra("key", "Value"); // you can pass data like this
context.sendBroadcast(intent); // fire broadcast receiver
}
Now, register your custom broadcast receiver from Activity
#Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter();
filter.addAction("CustomAction"); // use your custom action name
BroadcastReceiver updateUIReciver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//You will get your data here
//Update UI based on your data.
if (intent != null) {
String data = intent.getStringExtra("key");
}
}
};
registerReceiver(updateUIReciver, filter); // register broad cast receiver.
}
public class MainActivity2 extends AppCompatActivity {
EditText Timedisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main2);
Timedisplay = findViewById(R.id.editTextTime);
//register your broadcast here
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("messageevent"));
}
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, final Intent intent) {
// Get extra data included in the Intent
String message = intent.getStringExtra("key");
Log.e(TAG, "Got message: " + message);
if(message != null && !message.isEmpty()) {
//Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
String timedisplay = Timedisplay.getText();
}
}
};
//call this from anywhere you want to trigger the broadcoast
Intent i = new Intent("messageevent");
// You can also include some extra data.
i.putExtra("key", "value");
LocalBroadcastManager.getInstance(CheckUpdate.this).sendBroadcast(i);
Hi there i have a problem error that from NextActivity cannot go to the MainActivity3. Here is the Code NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView= findViewById(R.id.textView);
textView.setText(message);
}
public void onBtnClick(View view){
Intent intent = getIntent();
Intent intent1= new Intent(this,MainActivity3.class);
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
String message1 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE1);
TextView textView= findViewById(R.id.textView);
TextView plain1= findViewById(R.id.plain1);
TextView plain2= findViewById(R.id.plain2);
String b=plain1.getText().toString();
if(b.equals(message.toString())&&plain2.getText().toString().equals(message1.toString())){
startActivity(intent1);
}
else{
textView.setText("Password or Username is Wrong");
}
}
}
and here is the code of MainActivity3 that cannot be open through NextActivity
package com.example.java;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.view.ActionProvider;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity3 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent= getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
FrameLayout lay= (FrameLayout) findViewById(R.id.frames);
if(number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
}
else{
}
}
public void onBtnClick (View view){
Intent intent = new Intent(this,MainActivity2.class);
startActivity(intent);
}
}
The Problem is that through the NextActivity button click the apps will restart in it's own that supposed to go to the page of MainActivity3
I have fixed it by
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
Intent intent = getIntent();
String number = intent.getStringExtra(MainActivity2.extraint);
if(number != null) {
FrameLayout lay = (FrameLayout) findViewById(R.id.frames);
if (number.equals("1")) {
lay.setVisibility(View.INVISIBLE);
} else {
}
}
else{}
}
Maybe any others that have a better ways?
From the main page, i click the image and it will open the question 1. Now, I trying to open a new activity which is question 2 using image view from the question 1. But it has an error: question 1 is not an enclosing class.
Here is the code for Main Activity.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//button video to video page
b = (ImageView) findViewById(R.id.btnPlayVideo);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, VideoPage.class);
startActivity(i);
}
});
//button start test to question1
b = (ImageView) findViewById(R.id.btnStartTest);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Question1.class);
startActivity(i);
}
});
//button question 1 ke question 2
b = (ImageView) findViewById(R.id.btn1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
}
});}}
This is question 1.java. it said that it is not an enclosing class.
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}
This is question 2.java
package com.example.adhdtracker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Question2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question2);
}
}
Intent i = new Intent(Question1.this, Question2.class);
startActivity(i);
You are trying to start Question2 from the context of Question1 within MainActivity. The first parameter of new Intent() should be the context of the enclosing class that will start the next activity. Since you are defining this in MainActivity, the context should be MainActivity.this. There is a great introduction to this topic here.
Either change the first parameter of new Intent() to "MainActivity.this" or move the entire second OnClickListener to a view that is within Question1.
I have a page that has two buttons to "two" other pages, the thing is, button 1 and button 2 lead to a second page, thought button 1 should lead to the first page and button 2 should lead to the second page, here is the java file, I don't know how to put it!
ActiveMain.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button1;
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTwo.class);
// you made a mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
}
}
PageOne.java
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
}
}
PageTwo
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageTwo extends Activity {
Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weoff);
}
}
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageTWO.class); // you made a
mistake here you called the PageOne again here while you should call the second.
startActivity(intent);
}
});
here is my big problem:
http://www.bilder-space.de/show_img.php?img=7371f3-1333218887.jpg&size=original
and the code:
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
}
The error is:
-void is an invalid type for the variable onCLick()
Why does eclipse give me this errors?
you can use onclick inside your onCreate method like this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// your code here
}
});
}
You need to bring onClick out from onCreate method. Nested method declarations are not allowed in programming.
package org.madmax.sudoku;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Set up Click Listener for all Buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
You have defined onClick method inside of the onCreate method.
You can't have a method inside another method in Java. Put the onClick() after onCreate(), not inside it.