I want to access the folder with other folders and files to see them and press the back button to return to the main view.
But when I press the back button, it returns me to the previous folder in the directory I am in. Only if I give it several times back, it returns to main view.
Is there a way to force it back to main view by pressing the Back button only once or maybe some way to create a floating button to create a new Intent once ACTION_GET_CONTENT is used?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....
projectList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Just to see existing folders
Intent i = new Intent(Intent.ACTION_GET_CONTENT)
.setType("file/*");
setResult(RESULT_OK, i);
startActivityForResult(i, CODE_SEE_FOLDER);
}
});
}
...
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_CANCELED, intent);
startActivityForResult(intent, CODE_SEE_FOLDER);
super.onBackPressed();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CODE_SEE_FOLDER && requestCode == RESULT_OK){
Toast.makeText(context, "Your projects..", Toast.LENGTH_SHORT).show();
}else {
recreate();
}
}
Related
I have two activities, Activity1.java and Activity2.java. Activity1.java starts Activity2.java and I need Activity2.java to return some data back to Activity1.java when the user presses the back button on the Action Bar. But for some reason, it is not working...
Activity1.java:
public class Activity1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Intent intent = new Intent(this, Activity1.class);
int requestCode = 100;
startActivityForResult(intent, requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.i("TEST", "RequestCode:" + requestCode);
Log.i("TEST", "ResultCode:" + resultCode );
switch (resultCode) {
case RESULT_OK:
Log.i("TEST", data.getStringExtra("MESSAGE"));
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Activity2.java
public class Activity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("MESSAGE", "Hello from Activity 2!");
Log.i("TEST", "Setting result...");
setResult(RESULT_OK, intent);
finish();
super.onBackPressed();
}
}
When I run the app, only "Setting result..." is logged to Logcat. It seems like the onActivityResult override isn't even called. I've tried to change up the request code, result code, and setting the result in the onCreate() method of Activity2, but nothing works.
Could anybody help? Any help would be appreciated!
You are not calling the intent correctly:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
Intent intent = new Intent(this, Activity1.class);
int requestCode = 100;
startActivityForResult(intent, requestCode);
}
should be:
Intent intent = new Intent(this, Activity2.class);
You need to return the result when you press the ActionBar back/home button, but you added the code in the bottom back button.
So, transfer the code in Activity2 to be handled when the ActionBar back button is pressed, so Override onOptionsItemSelected, and the home button has an id of android.R.id.home.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Intent intent = new Intent(this, Activity1.class);
intent.putExtra("MESSAGE", "Hello from Activity 2!");
Log.i("TEST", "Setting result...");
setResult(RESULT_OK, intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
There are 3 problems with your code.
1. From Activity1, you start itself not Activity2. Change your code from
Intent intent = new Intent(this, Activity1.class);
to
Intent intent = new Intent(this, Activity2.class);
2. In onBackPressed() of Activity2, no need to declare Activity1 inside the intent, change your code from
Intent intent = new Intent(this, Activity1.class);
to
Intent intent = new Intent();
3. In Activity1, you should check requestCode before process further to make sure the data return from correct activity, because you might start more than one activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.i("TEST", "RequestCode:" + requestCode);
Log.i("TEST", "ResultCode:" + resultCode);
if (requestCode == 100) {
switch (resultCode) {
case RESULT_OK:
Log.i("TEST", data.getStringExtra("MESSAGE"));
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
I have a showAddForm button like the imej below in Claims.java.
When the + button is pressed, it will show Alert Dialog Window with radio buttons. When the radio button is checked, it will goes to specific activity. In the activity, it has an editText and a save button. I want the value on the editText display on the showAddForm button when the save button in the activity is clicked. How can I do to achieve this?
Claims.java
public class Claims extends Fragment {
private TextView c;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View claims = inflater.inflate(R.layout.claims, container, false);
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
AlertDialogRadio();
}
};
Button button1 = (Button) claims.findViewById(R.id.button10);
Button button = (Button) claims.findViewById(R.id.button8);
button1.setOnClickListener(listener);
c=(TextView)claims.findViewById(R.id.textView49);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
startActivity(intent);
}
});
return claims;
}
public void AlertDialogRadio() {
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivity(intent);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivity(intent);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivity(intent);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivity(intent);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivity(intent);
}
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("text");
c.setText(result);
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
Assume the user choose Project.
Project1.java
public class Project1 extends AppCompatActivity {
private static String text;
private static EditText txt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
txt= (EditText)findViewById(R.id.editText36);
Button b=(Button)findViewById(R.id.button17);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent returnIntent = new Intent();
text = txt.getText().toString();
returnIntent.putExtra("text", text);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
}
}
Now when I click save button in Project1.java, it return to the AlertDialogRadio which is not I want and the text still not displaying on the textView!!! ANYONE CAN HELP?????
You can use startActivityForResult() and override onActivityResult() in your activity. You can use setResult() in the second activity to pass whatever you want to your caller activity.
Example here and here
edit:
If you want it to use from a Fragment, you have to write this in your Activity that is holding the Fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
This will pass the result to your fragment, so you can handle the result inside your fragment.
And as I already mentioned, you have to use startActivityForResult() instead of startActivity()
as #keybee said you should use startActivityForResult() to start the project.class from your dialog.
In the Project1.Class when you click save use setResult() to send the value back to the claims.class(Dont do startActivity() again. just use finish() ).
and in your Claims.class use onActivityResult(int requestCode, int resultCode, Intent data) to recieve the value sent from the project1.class.
you can do a setText() on showAddForm button in the onActivityResult(int requestCode, int resultCode, Intent data) function
I have the following situation:
Parent Activity:
ParentActivityClass
{
private Intent intent;
#Override
public void onCreate(Bundle savedInstanceState)
{
.....
intent = new Intent(this, ChildActivity.class);
startActivityForResult(intent, 202);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
startActivity(intent);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.d("Log", "OK");
}
Child Activity
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
#Override
public void finish()
{
Intent intent = new Intent();
intent.putExtra("extra", ".....");
setResult(RESULT_OK, intent);
super.finish();
}
When calling the finish() method of child activity, onActivityResult is called in the parent activity. When the child activity is open for the 2nd time, onActivityResult is not called.
Where is the mistake?
The problem I see is that you are calling startActivity(intent) instead of startActivityForResult(intent, 202) within you View.OnClickListener.
Edit:
I'm assuming that you are going to the ChildActivity through the button.
Hope it helps ;)
Best Regards
In my quiz game, after wrong answer I call a popup with an Intent for result. In that popup I have an OK button. I need, after user press OK button, to load next question. But now, I see in background next question loaded, even though my popup is not closed. Here's my code:
static final int MY_REQUEST = 0;
Intent i = new Intent(Kviz.this, Popup_pogresno.class);
startActivityForResult(i, MY_REQUEST);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
nextQuestion();
}
And my popup class (it's a Theme.Dialog activity):
public class Popup_pogresno extends Activity implements OnClickListener{
Button ok;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
ok = (Button) findViewById(R.id.bPopupOK);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onBackPressed() {
//do nothing
}
}
So, I need the next question NOT to load UNTIL I press the OK button.
set a result code in Popup_pogresno Activity, and check this result code in Kviz activity on the basis of request code. If result code is ok the call the function nextQuestion()
set result code as follow and check in Kviz for is result code.
Intent i = new Intent(Kviz.this, Popup_pogresno.class);
startActivityForResult(i, MY_REQUEST);
set result code on ok button click in Popup_pogresno :
Intent returnIntent = new Intent();
setResult(RESULT_OK, returnIntent);
finish();
check for result code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_REQUEST) {
if(resultCode == RESULT_OK){
nextQuestion();
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
I have a TextView in my main activity.
pressing a button i open another activity and get to the options where i can rename my
TextView
btnSet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Options.class);
startActivity(intent);
}
});
In my Options activity i rename my TextView like this
public class Options extends MainActivity{
public static Boolean isRenamed;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
String lblName = ("Counter 1");
Button btnOk = (Button) findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent backToMain = new Intent(Options.this, MainActivity.class);
EditText labelName = (EditText) findViewById(R.id.set_name);
String lblName = labelName.getText().toString();
backToMain.putExtra(LABEL_NAME, lblName);
isRenamed = true;
startActivity(backToMain);
finish();
}
});
}
}
I am using the boolean isRenamed and i set it to true after clicking ok
And in my main activity i've set this
if (Options.isRenamed == true) {
// Get the message from the intent
Intent backToMain = getIntent();
String lblName = backToMain.getStringExtra(Options.LABEL_NAME);
// Edit the text view
TextView label = (TextView) findViewById(R.id.label1);
label.setText(lblName);
}
For now it's force closing, but is there some more elegant way to see if i've renamed the file and then execute that piece of code to set it?
Context.startActivityForResult();
do all your renaming and other stuff there. return a simple integer to know what happened. That way when you call finish() in your second activity it will return to your first without creating a new instance.
Use startActivityForResult() instead of startActivity(). In the Options Activity:
public static final int RENAMED = 100;
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent backToMain = new Intent(Options.this, MainActivity.class);
EditText labelName = (EditText) findViewById(R.id.set_name);
String lblName = labelName.getText().toString();
backToMain.putExtra(LABEL_NAME, lblName);
isRenamed = true;
setResult(RENAMED);
finish();
}
});
In MainActiviy, override onActivityResult() and
when it is fired check if resultCode paramter is equals to RENAMED .
for this senario you need to use
startActivityForResult(intent, requestCode)
instead of startActivity(intent)
in your Options Activity you ca setResult(resultCode, data) and call finish.
and in MainActivity you can use data in
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
you can use
startActivityForResult(intent, intentsData.REQUEST_CODE);
and then change your TextView when activity is resumed
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
/**
* change your TextView
*/
super.onActivityResult(requestCode, resultCode, data);
}