last commit
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package com.example.myapplication;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Toast;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
}
|
||||
|
||||
public void onClick(View v){
|
||||
Intent intent = new Intent(this, PuppetActivity.class);
|
||||
ResultHandler.addResultHandler(intent, () -> {
|
||||
Toast.makeText(this, "lol!", Toast.LENGTH_LONG).show();
|
||||
});
|
||||
startActivityForResult(intent, 0);
|
||||
//ResultHandler handler = new ResultHandler(this);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
ResultHandler handler = new ResultHandler(this);
|
||||
handler.onIncomingIntent(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.myapplication;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
|
||||
public class PuppetActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_pupet);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.myapplication;
|
||||
|
||||
public interface ResultAction {
|
||||
void action();
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.example.myapplication;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
public class ResultHandler {
|
||||
|
||||
private Context context;
|
||||
private static Map<Integer, ResultAction> map = new HashMap<>();
|
||||
private static final int RANDOM_BOUNDS = 256;
|
||||
private static final String LOG_TAG = "ResultHandler";
|
||||
|
||||
public static final String ACTION_ID_TITLE = "ACTION_ID";
|
||||
|
||||
public ResultHandler(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Статические поля кодов обработки входящих интентов
|
||||
*/
|
||||
public enum Fields{
|
||||
FILE_OPEN,
|
||||
}
|
||||
|
||||
public static void addResultHandler(Intent intent, ResultAction action){
|
||||
int random = (new Random()).nextInt(RANDOM_BOUNDS);
|
||||
intent.putExtra(ACTION_ID_TITLE, random);
|
||||
map.put(random, action);
|
||||
|
||||
}
|
||||
|
||||
public void onIncomingIntent(int requestCode, int resultCode, Intent data){
|
||||
if(data != null){
|
||||
if(data.hasExtra(ACTION_ID_TITLE)){
|
||||
int id = data.getExtras().getInt(ACTION_ID_TITLE);
|
||||
map.get(id).action();
|
||||
}else{
|
||||
Log.e(LOG_TAG, "Incoming intent haven't got ACTION_ID!!");
|
||||
throw new RuntimeException(
|
||||
"ResultHandler::RuntimeException can't parse incoming intent! Shutting down(("
|
||||
);
|
||||
}
|
||||
}else{
|
||||
Log.e(LOG_TAG, "Incoming intent is null!!");
|
||||
throw new RuntimeException(
|
||||
"ResultHandler::RuntimeException can't parse incoming intent! Shutting down(("
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user